sách gpt4 ăn đã đi

python - 如何从无限滚动网站上抓取所有内容?抓取

In lại 作者:太空狗 更新时间:2023-10-29 22:21:35 29 4
mua khóa gpt4 giày nike

我正在使用 scrapy。

我正在使用的网站具有无限滚动功能。

该网站有很多帖子,但我只抓取了 13 个。

如何抓取剩余的帖子?

Đây là mã của tôi:

class exampleSpider(scrapy.Spider):
name = "example"
#from_date = datetime.date.today() - datetime.timedelta(6*365/12)
allowed_domains = ["example.com"]
start_urls = [
"http://www.example.com/somethinghere/"
]

def parse(self, response):
for href in response.xpath("//*[@id='page-wrap']/div/div/div/section[2]/div/div/div/div[3]/ul/li/div/h1/a/@href"):
url = response.urljoin(href.extract())
yield scrapy.Request(url, callback=self.parse_dir_contents)


def parse_dir_contents(self, response):
#scrape contents code here

câu trả lời hay nhất

检查网站代码。

如果无限滚动自动触发 js 操作,您可以使用 Alioth 提案进行如下操作:spynner

跟随 spynner tài liệu ,你会发现可以触发 jquery 事件。

Look up the library code to see which kind of events you can fire.

尝试在网站可滚动内容内的任何 div 上生成滚动到底部事件或创建 css 属性更改。关注 spynner tài liệu ,类似于:

browser = spynner.Browser(debug_level=spynner.DEBUG, debug_stream=debug_stream)
# load here your website as spynner allows
browser.load_jquery(True)
ret = run_debug(browser.runjs,'window.scrollTo(0, document.body.scrollHeight);console.log(''scrolling...);')
# continue parsing ret

无限滚动不太可能由 anchor 链接触发,但可能可以由 jquery 操作触发,不一定附加到链接。对于这种情况,请使用如下代码:

br.load('http://pypi.python.org/pypi')

anchors = br.webframe.findAllElements('#menu ul.level-two a')
# chooses an anchor with Browse word as key
anchor = [a for a in anchors if 'Browse' in a.toPlainText()][0]
br.wk_click_element_link(anchor, timeout=10)
output = br.show()
# save output in file: output.html or
# plug this actions into your scrapy method and parse output var as you do
# with response body

然后,在 output.html 文件上运行 scrapy,或者,如果您是这样实现的,则使用您选择的本地内存变量来存储 js 操作后修改后的 html。

作为另一种解决方案,您尝试解析的网站可能有一个替代呈现版本,以防访问者浏览器KHÔNG js 激活。

尝试使用禁用 javascript 的浏览器来呈现网站,也许这样,网站会在内容部分的末尾提供一个 anchor 链接。

还有使用 Scrapy 和 Selenium 的方法成功爬虫 js 导航的实现,详见 cái này所以回答。

关于python - 如何从无限滚动网站上抓取所有内容?抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37207959/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress