XPath 爬蟲學習記錄。爬取知名平台熱榜

2021-10-05 06:57:51 字數 3989 閱讀 3481

為了滿足自己看一些時下熱門的新聞的需求,但又覺得開啟每個平台再去翻找有點麻煩。

正好順便學了一下爬蟲。

說明:python語言,xpath方法,windows平台

先宣告一下自己匯入的庫

我爬取的是知乎、微博、貼吧的熱門

知乎熱榜:

url_zhihu=""

微博熱搜:

url_weibo=""

貼吧熱議:

url_tieba=""

此處僅貼了爬取知乎的**。其他**大同小異,請自行更改

ht=urlopen(url_zhihu).read().decode("utf-8")

html=etree.html(ht)

使用xpath路徑爬取內容說明:路徑分為絕對路徑和相對路徑。

使用絕對路徑時每一級的標籤都不能漏掉比如("/html/body/........./text()")。使用相對路徑的格式(比如:"//div[@class="td"]")

此處爬取知乎熱榜如下

path="//div[@class='hotlist-itemtitle']"

result=html.xpath(path)

採用的是相對路徑,查詢屬性class='hotlist-itemtitle'的div標籤

然後使用xpath方法獲取結果。

自己將內容提取後並存入了乙個檔案

print(" ")

print("**********知乎熱榜前20**********")

print(" ")

with open(file_name,"w+",encoding="utf-8") as f:

for i in range(20):

try:

print(str(i+1)+"."+result[i].text)

print(" ")

string=str(i+1)+"."+result[i].text+"\n"

f.writelines(string)

except:

print("something wrong!")

至此,乙個簡單的爬蟲就做好了

from urllib.request import urlopen

from lxml import etree

#知乎def zhihu(file_name):

url_zhihu=""

ht=urlopen(url_zhihu).read().decode("utf-8")

html=etree.html(ht)

path="//div[@class='hotlist-itemtitle']"

result=html.xpath(path)

print(" ")

print("**********知乎熱榜前20**********")

print(" ")

with open(file_name,"w+",encoding="utf-8") as f:

for i in range(20):

try:

print(str(i+1)+"."+result[i].text)

print(" ")

string=str(i+1)+"."+result[i].text+"\n"

f.writelines(string)

except:

print("something wrong!")

#微博

def weibo(file_name):

url_weibo=""

ht=urlopen(url_weibo).read().decode("utf-8")

html=etree.html(ht)

path="//td[@class='td-02']/a[@target='_blank']/text()"

path_href="//td[@class='td-02']/a[@target='_blank']/@href"

result=html.xpath(path)

result_href=html.xpath(path_href)

print(" ")

print("**********微博熱搜前20**********")

print(" ")

#del(result[0])

#del(result_href[0])

with open(file_name,"w+",encoding="utf-8") as f:

for i in range(10):

try:

print(str(i+1)+"."+result[i+1])

print(""+result_href[i+1])

print(" ")

string=str(i+1)+"."+result[i+1]+"\n"+""+result_href[i+1]+"\n"+"\n"

f.writelines(string)

except:

print("something wrong!")

#貼吧def tieba(file_name):

url_tieba=""

ht=urlopen(url_tieba).read().decode("utf-8")

html=etree.html(ht)

path_title="//div[@class='topic-name']/a/text()"

path_href="//div[@class='topic-name']/a/@href"

path_desc="//p[@class='topic-top-item-desc']/text()"

result_title=html.xpath(path_title)

#print(result_title)

result_href=html.xpath(path_href)

result_desc=html.xpath(path_desc)

print(" ")

print("**********貼吧熱議前20**********")

print(" ")

with open(file_name,"w+",encoding="utf-8") as f:

for i in range(20):

try:

print(str(i+1)+"."+result_title[i])

print(" "+result_href[i])

print("desc:"+result_desc[i])

print(" ")

string=str(i+1)+"."+result_title[i]+"\t"+"\n\tdesc:"+result_desc[i]+"\n"+"\t"+result_href[i]+"\n"+"\n"

f.writelines(string)

except:

print("something wrong!")

file_name="各平台熱榜.docx" #此處本打算將三個檔案合併到同乙個檔案,但困的不得了就不做了

file_name_zhihu="知乎熱榜top20_實時.docx"

file_name_weibo="微博熱搜top20_實時.docx"

file_name_teiba="貼吧熱議top20_實時.docx"

zhihu(file_name_zhihu)

weibo(file_name_weibo)

tieba(file_name_teiba)

本打算將三個檔案合併到同乙個檔案,但困的不得了就不做了。

新人入門,歡迎各路大神的不嗇吝教

datawhale爬蟲(xpath爬取丁香網評論)

1.xpath基礎學習 前面我們介紹了 beautifulsoup 的用法,這個已經是非常強大的庫了,不過還有一些比較流行的解析庫,例如 lxml,使用的是 xpath 語法,同樣是效率比較高的解析方法。如果大家對 beautifulsoup 使用不太習慣的話,可以嘗試下 xpath。xpath 是...

爬蟲專欄3 xpath爬取貓眼

from lxml import etree import requests import time url headers response requests.get url,headers headers html response.text movie name xpath s etree.h...

爬蟲 學習爬取表情包

表情包位址 表情位址 1.請求目標 2.匹配不同位址 url def get urls url 1.請求目標 response requests.get url 2.通過正則來匹配不同位址 url 每張共有內容保留,不同的內容用.來匹配 表示匹配任意數量不換行的字元 表示盡可能匹配最短的字元 r u...