html - bs4如何同时抓取多个页面?

标签 html python-3.x beautifulsoup praw

我想收集关于 reddit 的评论,我使用 praw 来获取像 a2rp5i 这样的文档的 ID。例如,我已经收集了一组ID,如

docArr=
['a14bfr', '9zlro3', 'a2pz6f', 'a2n60r', 'a0dlj3']
my_url = "https://old.reddit.com/r/Games/comments/a0dlj3/"
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()
page_soup = soup(page_html, "html.parser")
content_containers = page_soup.findAll("div", {"class":"md"})
timestamp_containers = page_soup.findAll("p", {"class":"tagline"})
time = timestamp_containers[0].time.get('datetime')

我想使用时间作为我的文件名,我想将内容保存为 txt 文件

outfile = open('%s.txt' % time , "w") 
for content_container in content_containers:
    if content_container == "(self.games)":
        continue
    data = content_container.text.encode('utf8').decode('cp950', 'ignore')
    outfile.write(data)
outfile.close()

这种尝试对我来说很好,只保存一个 url 但是我想同时在 docArr 中保存 ID

url_test = "https://old.reddit.com/r/Games/comments/{}/"
for i in set(docArr):
    url = url_test.format(i)

它为我提供了正确的网址。但是如何一次性保存 docArr 中所有 url 的 timecontent_container

最佳答案

你只需要在当前代码中添加缩进

for i in docArr:
    url = url_test.format(i)
    uClient = uReq(url)
    ....
    ....
    outfile = open('%s.txt' % time , "w") 
    for content_container in content_containers:
        ....
        ....
    outfile.close()

关于html - bs4如何同时抓取多个页面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53648789/

相关文章:

Python BeautifulSoup 检查输出

html - 如何将 OpenOffice Writer 文档 (.odt) 转换为多个带导航的 HTML 文件?

php - 通过 POST 提交时删除某些字符

jquery - 如何修复 .slideToggle 过渡或动画

python - 避免 python 范围错误的策略

python - 检查一个 Python 脚本是否使用另一个 Python 脚本进行编译

python - 从每个列表中删除第三个元素和第二个元素,并查找二维列表中最后一个元素在特定范围内的列表

python - 如何使用 beautifulsoup 为 html 嵌套标签定义 findAll

python - 抓取的网络数据中缺少信息,谷歌翻译,使用Python

html - Css 响应图像问题