为什么这个网站不能用 bs4 抓取?



我是学习网络抓取的初学者,由于某种原因我无法抓取这个网站。当我在Chrome中检查它时,代码看起来不错,但是当我使用BeautifulSoup阅读它时,它不再可抓取。汤提到了"谷歌分析",我真的不知道那是什么。

网站的内容是通过JavaScript加载的,但您可以使用requests模块来获取各个章节。章节的 URL 采用形式https://detroitbecometext.github.io/assets/html/chapterXY.html(示例(。

例如此 srip:

import re
import requests
from bs4 import BeautifulSoup

url = 'https://detroitbecometext.github.io/chapters'
asset_url = 'https://detroitbecometext.github.io/assets/html/'
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
main_js = requests.get('https://detroitbecometext.github.io/' + soup.select_one('script[src^="main."]')['src']).text
for ch in re.findall(r'(chapter[d.]+.html?)', main_js):
soup = BeautifulSoup(requests.get(asset_url + ch).content, 'html.parser')
print(soup.get_text())
print('-' * 80)

打印所有章节中的所有文本:

...

Out of the elevator
SWAT: Negotiator on site. Repeat, negotiator on site.
Caroline Phillips: No, stop... I... I... I can't leave her. Oh, oh please, please, you gotta save my little girl... Wait... you're
sending an android?
SWAT: Alright, ma'am. We need to go.
Caroline Phillips: You can't...you can't do that! You W- Why aren't you sending a real
person? Don't let that thing near her! Keep that thing away from my daughter! KEEP IT AWAY!

...

最新更新