python語言網路爬蟲學習(二)

2021-10-07 22:20:44 字數 1383 閱讀 6629

這一章節主要寫如何將python爬取的內容儲存在json檔案和mysql資料庫。

import requests

from bs4 import beautifulsoup

rqq = requests.get(

'')#http請求

soup = beautifulsoup(rqq.content,

'lxml'

)#轉換格式

soup.select(

'#topwords'

)#可以檢視id屬性為topwords的

dat = soup.select(

'.hot-news > li > a'

)#[i.text for i in soup.select('.hot-news > li > a')] #提取內容

#[i['title'] for i in soup.select('.hot-news > li > a')]

names =

[i.text for i in dat]

href =

[i['href'

]for i in dat]

#提取指向**

print

(names, href)

import json

with

open

('./temp.json'

,'w'

)as f:

json.dump(

, f, ensure_ascii=

false

)

可以使用記事本或者notepad++開啟檔案都沒有問題,並且json檔案就是以字典的形式展現的。

#需要提前建立資料庫

在將我們爬取內容儲存在資料庫中是遇到了一點麻煩,這個問題我在網上看到很多小夥伴也沒有搞定,所以就將這個報錯寫在這個部落格:寫進資料庫報錯無法解決

Python網路爬蟲學習(二)

十五.京東商品頁面的爬取 import requests r requests.get r.status code r.encoding r.text 1000 十六.亞馬遜商品頁面的爬取 import requests def main url try kv r requests.get url,...

python網路爬蟲(二)

在第一篇中,我們介紹了如何進行發起乙個http請求,並接受響應。在這一部分中,我們介紹一下如何解析網頁並提取我們需要的資料。我們採用requests這個庫進行乙個網頁請求。r requests.get headers,kwargs 通過這一句 我們即可獲得伺服器傳給我們的響應內容 不考慮連線錯誤等情...

Python網路爬蟲學習

最近有時間學習在慕課網上跟著嵩天老師上他的python網路爬蟲與資訊提取這門課,想著可以寫些部落格將學的爬蟲知識總結起來。win平台下 前提是安裝好python,在cmd中執行 pip installl requests 其他方法的話可以在網上搜尋。r requests.get url 其中get返...