1
我有一個熱門文章的網頁,我想爲每個引用的網頁的超鏈接和它顯示的文章的標題進行刮擦。利用BeautifulSoup爲鏈接標題和URL刮頁面
我的腳本所需的輸出是一個CSV文件,它將每個標題和文章內容列在一行中。所以如果這個網頁上有50篇文章,我想要一個文件有50行和100個數據點。
我的問題在於,文章標題及其超鏈接包含在一個SVG容器中,它將我拋棄。我之前使用過BeautifulSoup進行網頁抓取,但不知道如何選擇每篇文章的標題和超鏈接。任何和所有的幫助,非常感謝。
import requests
from bs4 import BeautifulSoup
import re
res = requests.get('http://fundersandfounders.com/what-internet-thinks-based-on-media/')
res.raise_for_status()
playFile = open('top_articles.html', 'wb')
for chunk in res.iter_content(100000):
playFile.write(chunk)
f = open('top_articles.html')
soup = BeautifulSoup(f, 'html.parser')
links = soup.select('p') #i know this is where i'm messing up, but i'm not sure which selector to actually utilize so I'm using the paragraph selector as a place-holder
print(links)
我知道,這實際上是一個兩步項目:我的腳本的當前版本不通過其實際內容,我將要刮所有超鏈接的列表進行迭代。這是我自己可以輕鬆執行的第二步,但是如果有人也想寫這些,那麼對你很有好處。
感謝您的快速響應。我要安裝哪個模塊(用於Python3)以利用urllib.parse和urljoin?我似乎無法找到它。 – dataelephant
@Harelephant'urllib'是內置的,不需要安裝它。 – alecxe