小何的爬蟲筆記

2021-10-10 11:18:40 字數 3489 閱讀 7861

import requests

# 使用方法\ request的編碼流程

# 指定url

# 發起請求

# 獲取相應資料

# 持久化儲存

# unit1

url1=

''data=requests.get(url=url1)

result1=data.text

print

(len

(result1)

)# unit2

url2=

'web?'

# 處理url攜帶的引數:封裝到字典

# ua偽裝

headers_firefox=

kw=input

('enter a word:'

)param=

data=requests.get(url=url2,params=param,headers=headers_firefox)

result2=data.text

filename=kw +

'.html'

with

open

(filename,

'w',encoding=

'utf-8'

)as fp:

fp.write(result2)

print

(filename,

'儲存成功'

)# 實戰鞏固

# ——post請求(攜帶了引數)

headers_google=

post_url3=

''# post請求和get請求一樣

word=

input

('enter a word:'

)data=

response1=requests.post(url=post_url3,data=data,headers=headers_google)

dict_obj=response1.json(

)# json返回的是obj(如果確定伺服器響應型別是json型別的,才可以使用json)

print

(dict_obj)

# 持久化儲存

import json

filename=word+

'.json'

fp=open

(filename,

'w',encoding=

'utf-8'

)json.dump(dict_obj,fp=fp,ensure_ascii=

false

)print

('fanyi___over'

)# unit4

# 爬取豆瓣電影中的詳情資料

# 阿賈克斯請求

post_url4=

''param=

response2=requests.get(url=post_url4,params=param,headers=headers_google)

list_data=response2.json(

)fp=

open

('./douban.json'

,'w'

,encoding=

'utf-8'

)json.dump(list_data,fp=fp,ensure_ascii=

false

)print

('douban___over'

)

專案案例4:爬取國家藥品監督管理總局中基於中華人民共和國化妝品生產許可證的相關資料

import requests

import re

import json

"""專案案例4:爬取國家藥品監督管理總局中基於中華人民共和國化妝品生產許可證的相關資料

"""headers_firefox=

headers_google=

"""# with open('./1.html','w',encoding='utf-8') as fp:

# fp.write(data)

# print('over')

從此可以發現有些資料是動態載入過來的(例如阿賈克斯資料)

發現首頁中對應的企業資訊資料是通過ajax動態請求到的

通過對詳情頁的觀察發現:url網域名稱是一樣的,只有攜帶的引數(id)不是一樣的

id可以從首頁中對應的ajax請求到的json串中獲取

網域名稱和id拼接處乙個完整企業對應的url

詳情頁的資料也是動態載入過來的

發現所有post請求的url都是一樣的,只有引數id是不同的

可以批量獲取多家企業的id,然後合併id和url獲得所需要的資料

"""# 第一步:批量獲取id位址

url=

''# 引數的封裝

id_list =

for page in

range(0

,25):

page=

str(page)

param=

json_ids=requests.post(url=url,data=param,headers=headers_google)

.json(

)for dic in json_ids[

'list']:

'id'])

# id結合url 獲取企業詳情資料

post_url=

""all_datalist=

forid

in id_list:

data=

detail_json=requests.post(url=post_url,headers=headers_google,data=data)

.json(

)fp=

open

('./alldata.json'

,'w'

,encoding=

'utf-8'

)json.dump(all_datalist,fp=fp,ensure_ascii=

false

)print

(len

(all_datalist)

)print

('over'

)

import requests

post_url=

''kw=input

("please input a word:\n"

)headers=

data=

data=requests.post(post_url,data=data)

filename=kw+

'.html'

with

open

(filename,

'w',encoding=

'utf-8'

)as fp:

fp.write(data.text)

print

('end'

)

小何的爬蟲筆記 資料解析

聚焦爬蟲 爬取頁面中指定的頁面內容 編碼流程 1.指定url 2.發起請求 3.獲取響應資料 4。將響應資料進行持久化儲存 資料解析分類 正則 bs4 xpath 資料解析原理 解析的區域性的文字內容都會在標籤之間或者標籤對應的屬性中進行儲存 1.進行指定標籤定位 2.標籤或者標籤對應的屬性中儲存的...

小爬蟲系列

玩玩小爬蟲 抓取時的幾個小細節 摘要 這一篇我們聊聊在頁面抓取時應該注意到的幾個問題。一 網頁更新 我們知道,一般網頁中的資訊是不斷翻新的,這也要求我們定期的去抓這些新資訊,但是這個 定期 該怎麼理解,也就是多長時間需要抓一次該頁面,其實這個定期也就是頁面快取時間,在頁面的快取時間內我們再次抓取該網...

入門小爬蟲

python爬蟲最基本流程是,獲取請求,解析頁面,處理頁面資料 python獲取頁面的的第三方庫很多,像request,urllib,解析方法有最簡單的re正規表示式,也有專門用來解析的庫xmlx,beautifulsoup等。下面以request,正規表示式為例。一 獲取請求,得到網頁文字,先上 ...