Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Python抓取加载Javascript的页面_Python_Web Scraping - Fatal编程技术网

使用Python抓取加载Javascript的页面

使用Python抓取加载Javascript的页面,python,web-scraping,Python,Web Scraping,我正试图用Python来浏览BBC文章中的评论: comments模块是Javascript的,带有用于下一页的按钮。但是,我找不到AJAX url—在网络控制台上查看有一个链接,但这不起作用: 但是,我想刮取多个页面,但是当我尝试更改第一页中的“page=x”时,它只会将我带到第一页 我曾想过使用Selenium/Dryscape,但我不确定如何进入每个页面来运行它们。您可以通过右键单击网络部分中的xhr请求来尝试复制为Curl命令 这就是我得到的 curl "https://ssl.bbc.

我正试图用Python来浏览BBC文章中的评论:

comments模块是Javascript的,带有用于下一页的按钮。但是,我找不到AJAX url—在网络控制台上查看有一个链接,但这不起作用:

但是,我想刮取多个页面,但是当我尝试更改第一页中的“page=x”时,它只会将我带到第一页


我曾想过使用Selenium/Dryscape,但我不确定如何进入每个页面来运行它们。

您可以通过右键单击网络部分中的xhr请求来尝试
复制为Curl
命令

这就是我得到的

curl "https://ssl.bbc.co.uk/modules/comments/ajax/comments/?siteId=newscommentsmodule^&forumId=__CPS__37750489^&filter=none^&sortOrder=Descending^&sortBy=Created^&mock=0^&mockUser=^&parentUri=http^%^3A^%^2F^%^2Fwww.bbc.com^%^2Fnews^%^2Feducation-37750489^%^2Fcomments^%^3Fcomments_page^%^3D1^%^26initial_page_size^%^3D10^%^26filter^%^3Dnone^%^26sortBy^%^3DCreated^%^26sortOrder^%^3DDescending^&loc=en-GB^&preset=responsive^&initial_page_size=10^&transTags=0^&comments_page=4" -H "Origin: http://www.bbc.com" -H "Accept-Encoding: gzip, deflate, sdch, br" -H "Accept-Language: en-US,en;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.44 Safari/537.36" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: http://www.bbc.com/news/education-37750489/comments?comments_page=1^&initial_page_size=10^&filter=none^&sortBy=Created^&sortOrder=Descending" -H "Cookie: BBC-UID=f5b82387c6a5f59cfb1e4e702165956d3c4a59fc40f0a0cc2289331f59e23c7f0Mozilla^%^2f5^%^2e0^%^20^%^28Windows^%^20NT^%^2010^%^2e0^%^3b^%^20Win64^%^3b^%^20x64^%^29^%^20AppleWebKit^%^2f537^%^2e36^%^20^%^28KHTML^%^2c^%^20like^%^20Gecko^%^29^%^20Chrome^%^2f55^%^2e0^%^2e2883^%^2e44^%^20Safari^%^2f537^%^2e36; BGUID=e50803c74685961b76a3bae761e263da9bbf269019d8d4abbed18707f72c1098; s1=208.5.385837657400859000FDD0E52985" -H "Connection: keep-alive" --compressed
也可以使用selenium直接单击分页按钮

driver.find_element_by_css_selector('li.comments-pagination-page.comments-pagination-page-{} a'.format(pageNumber)).click()

这里的
li.comments分页页面。comments-pagination-page-3
是页面中第三个分页按钮的li标记。

您可以通过右键单击网络部分中的xhr请求来尝试
复制为Curl
命令

这就是我得到的

curl "https://ssl.bbc.co.uk/modules/comments/ajax/comments/?siteId=newscommentsmodule^&forumId=__CPS__37750489^&filter=none^&sortOrder=Descending^&sortBy=Created^&mock=0^&mockUser=^&parentUri=http^%^3A^%^2F^%^2Fwww.bbc.com^%^2Fnews^%^2Feducation-37750489^%^2Fcomments^%^3Fcomments_page^%^3D1^%^26initial_page_size^%^3D10^%^26filter^%^3Dnone^%^26sortBy^%^3DCreated^%^26sortOrder^%^3DDescending^&loc=en-GB^&preset=responsive^&initial_page_size=10^&transTags=0^&comments_page=4" -H "Origin: http://www.bbc.com" -H "Accept-Encoding: gzip, deflate, sdch, br" -H "Accept-Language: en-US,en;q=0.8" -H "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.44 Safari/537.36" -H "Accept: application/json, text/javascript, */*; q=0.01" -H "Referer: http://www.bbc.com/news/education-37750489/comments?comments_page=1^&initial_page_size=10^&filter=none^&sortBy=Created^&sortOrder=Descending" -H "Cookie: BBC-UID=f5b82387c6a5f59cfb1e4e702165956d3c4a59fc40f0a0cc2289331f59e23c7f0Mozilla^%^2f5^%^2e0^%^20^%^28Windows^%^20NT^%^2010^%^2e0^%^3b^%^20Win64^%^3b^%^20x64^%^29^%^20AppleWebKit^%^2f537^%^2e36^%^20^%^28KHTML^%^2c^%^20like^%^20Gecko^%^29^%^20Chrome^%^2f55^%^2e0^%^2e2883^%^2e44^%^20Safari^%^2f537^%^2e36; BGUID=e50803c74685961b76a3bae761e263da9bbf269019d8d4abbed18707f72c1098; s1=208.5.385837657400859000FDD0E52985" -H "Connection: keep-alive" --compressed
也可以使用selenium直接单击分页按钮

driver.find_element_by_css_selector('li.comments-pagination-page.comments-pagination-page-{} a'.format(pageNumber)).click()

这里的
li.comments分页页面。comments-pagination-page-3
是页面中第三个分页按钮的li标签。

“我曾想过使用Selenium/Dryscape,但我不确定如何进入每个页面来运行它们。”你能解释一下吗?我不明白这个问题。我需要找到一种方法来获取每个评论页面的URL,以便运行scrape。我曾想过运行Selenium或Dryscape之类的屏幕刮板,但我仍然需要获得一个URL来访问每个页面,对吗?分享您迄今为止尝试过的代码“我曾想过使用Selenium/Dryscape,但我不确定如何访问每个页面来运行它们。”您能详细解释一下吗?我不明白这个问题。我需要找到一种方法来获取每个评论页面的URL,以便运行scrape。我正在考虑运行Selenium或DrySrape之类的屏幕刮板,但我仍然需要获得一个URL才能首先访问每个页面,对吗?共享您迄今为止尝试过的代码如果使用驱动程序
单击()
标记
,会话中的页面将发生更改,因此您不需要URL。如果
单击()
使用驱动程序的
标记会话中的页面将更改,因此您不需要URL。