【发布时间】:2017-01-09 21:00:37
【问题描述】:
我有一个热门文章的网页,我想为每个引用网页的超链接和它正在显示的文章的标题抓取。
我的脚本所需的输出是一个 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)
我知道这实际上是一个两步项目:我的脚本的当前版本不会遍历我将要抓取其实际内容的所有超链接的列表。这是我可以自己轻松执行的第二步,但是如果有人也想写那部分,向你表示敬意。
【问题讨论】:
标签: python html text web-scraping beautifulsoup