爬蟲常用筆記總結-創(chuàng)新互聯(lián)

方法

創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設、高性價比長治網(wǎng)站開發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫,直接使用。一站式長治網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設找我們,業(yè)務覆蓋長治地區(qū)。費用合理售后完善,十多年實體公司更值得信賴。
import requests
from bs4 import BeautifulSoup
from fake_useragent import FakeUserAgent

hd = {'User-Agent': FakeUserAgent().random}
r = requests.get('https://www.meishij.net/fenlei/zaocan/', headers=hd)
# r.status_code   # 查看狀態(tài)
# r.encoding  # 編碼 ISO-8859-1
r.encoding = 'UTF-8'  # 轉(zhuǎn)換編碼格式
soup = BeautifulSoup(r.text, 'html.parser')  # r.content

"""

"""
# 指定標簽
alst = soup.find_all('table')
for i in alst:
    print(i.a.get('title'), i.span.string, '\t', i.p.string)

# 指定樣式 class_
info_all = soup.find_all('div', class_='list_s2_item')
for i in info_all:
    info1 = i.find('a', class_='list_s2_item_info').strong.string
    info2 = i.find('a', class_='list_s2_item_info').span.string

# 指定元素
address = soup.select(
    'body >div.comm-content-box.clearfloat >div >div.comm-content-left.clearfloat >div >div.xq_toggle >div:nth-child(2) >table:nth-child(1) >tbody >tr:nth-child(2) >td:nth-child(2)')
print(address[0].string)

"""
獲取內(nèi)容語法
data.get_text()   # 獲取data中文本內(nèi)容
p.string  #

張三

a.get('title') # title='張三' """

借用網(wǎng)站功能查IP地址:

import requests
from bs4 import BeautifulSoup
from fake_useragent import FakeUserAgent


def get_address_by_ip(ip):
    hd = {'User-Agent': FakeUserAgent().random}
    url = 'https://ip.hao86.com/' + ip + '/'
    r = requests.get(url, headers=hd)
    r.encoding = 'UTF-8'
    soup = BeautifulSoup(r.text, 'html.parser')
    address = soup.select(
        'body >div.comm-content-box.clearfloat >div >div.comm-content-left.clearfloat >div >div.xq_toggle >div:nth-child(2) >table:nth-child(1) >tbody >tr:nth-child(2) >td:nth-child(2)')
    return address[0].string


IP = input('輸入你要查詢的IP:')  # 221.218.142.209
address = get_address_by_ip(IP)
print(address)

保存圖片:

import requests
import os

'''
爬取指定url圖片
可能出現(xiàn)的問題:圖片地址不合法,缺少http
http://upload.techweb.com.cn/s/310/2018/0410/1523330175838.jpg
'''
url4 = 'http://img.netbian.com/file/2022/1127/small1208244iXg61669522104.jpg'

theUrl = url4  # 指定要爬取的url

# 爬取到的圖片存在電腦那個磁盤位置
root = "d://pythonZoneHH//pic2HH//"

path = root + theUrl.split("/")[-1]  # 這句話可以保證原來圖片叫什么名字,爬下來時候還叫什么名字
print(path)
try:
    if not os.path.exists(root):  # 判斷磁盤制定文件夾是否存在,
        os.makedirs(root)  # 如果不存在就創(chuàng)建文件夾

    r = requests.get(theUrl)
    print("文件大小", len(r.content) / 1024, "kb")
    with open(path, "wb") as f:
        print("正在保存文件...")
        f.write(r.content)  # 向文件中寫入二進制內(nèi)容
        print("文件保存成功")
except Exception as e:
    print("爬取失敗", e)

頁面中不存在的數(shù)據(jù)爬?。?/p>

右鍵檢查,從網(wǎng)絡的請求中的找到對應數(shù)據(jù),在標頭中復制出URL

import json

from bs4 import BeautifulSoup
import requests
from fake_useragent import FakeUserAgent

re_url = 'https://www.thecfa.cn/index.html'
url = 'https://www.thecfa.cn/nvzgjd/dataApi/text/v1?contId=30235&next=-1'
hd = {'User-Agent': FakeUserAgent().random, 'Referer': re_url}  # Referer:re_url 設置引用頁
r = requests.get(url, headers=hd)
r.encoding = 'UTF-8'
# r.text 獲取到的是一個字符串  {"text":" 國際足聯(lián)排名..."}  類型:dic = json.loads(r.text)  # 字符串轉(zhuǎn)json數(shù)據(jù)  用 json.loads(內(nèi)容)
html_doc = dic.get('text')  # 通過 .get('鍵')  取出值即可
soup = BeautifulSoup(html_doc, 'html.parser')
info_all = soup.find_all('li')
for i in info_all:
    # print(i, '....')
    date = i.find('div', class_='date').get_text()
    img_url = i.find_all('div', class_='country')[0].p.img.get('src')
    score = i.find('div', class_='score').get_text()
    img_url2 = i.find_all('div', class_='country')[1].p.img.get('src')
    print(date, '\t', img_url, '\n', score, '\t', img_url2)

你是否還在尋找穩(wěn)定的海外服務器提供商?創(chuàng)新互聯(lián)www.cdcxhl.cn海外機房具備T級流量清洗系統(tǒng)配攻擊溯源,準確流量調(diào)度確保服務器高可用性,企業(yè)級服務器適合批量采購,新人活動首月15元起,快前往官網(wǎng)查看詳情吧

網(wǎng)頁標題:爬蟲常用筆記總結-創(chuàng)新互聯(lián)
鏈接分享:http://muchs.cn/article42/coisec.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供虛擬主機、網(wǎng)站排名外貿(mào)建站、微信小程序、定制網(wǎng)站、App開發(fā)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設