資源簡介
此資源用xpath的方法來解析網頁的內容,詳細的介紹了下載網頁、解析數據、將數據存入表格的過程。希望能給到你借鑒。
代碼片段和文件信息
#?-*-?coding:utf-8?-*-
import?requests
from?fake_useragent?import?UserAgent
agent=UserAgent()
#當用到xpath時需要引入此包
from?lxml?import?etree
#下載
url=“http://search.51job.com/list/010000%252C020000%252C180200%252C200200000000000000999python21.html?lang=c&stype=&postchannel=0000&workyear=99&cotype=99°reefrom=99&jobterm=99&companysize=99&providesalary=99&lonlat=0%2C0&radius=-1&ord_field=0&confirmdate=9&fromType=&dibiaoid=0&address=&line=&specialarea=00&from=&welfare=“
response?=?requests.get(url
??????????????????????????????????????????headers?=?{“User-Agent“:agent.random}
??????????????????????)
#設置編碼格式
response.encoding=response.apparent_encoding
#?解析
#?root可理解為網頁本身
root?=?etree.HTML(response.text)
#用xpath返回的是一個列表
div_list?=?root.xpath(‘//div[@class=“dw_table
- 上一篇:Python爬取小說網站信息并存儲到數據庫
- 下一篇:cpso py文件代碼
評論
共有 條評論