无法从Quora网页中获取许多问题

2024-04-24 08:17:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在学习BeautifulSoup,并尝试从thisQuora页面上获取不同问题的链接。在

当我向下滚动网页时,网页中出现的问题不断出现并显示出来。在

当我试着用下面的代码来获取这些问题的链接时,我只得到了5个链接。ie-我只得到5个问题的链接,即使有很多问题在网站上。在

有没有什么解决办法可以让网页中出现的问题链接尽可能多?在

from bs4 import BeautifulSoup
import requests

root = 'https://www.quora.com/topic/Graduate-Record-Examination-GRE-1'
headers = {'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X x.y; rv:42.0) Gecko/20100101 Firefox/42.' }
r = requests.get(root,headers=headers)

soup = BeautifulSoup(r.text,'html.parser')

q = soup.find('div',{'class':'paged_list_wrapper'})
no=0
for i in q.find_all('div',{'class':'story_title_container'}):
    t=i.a['href']
    no=no+1
    print(root+t,'\n\n')

Tags: no代码importdiv网页链接root页面
1条回答
网友
1楼 · 发布于 2024-04-24 08:17:36

你所要达到的目标是无法用请求和美化团队来完成的。你需要用硒。 这里我用硒和铬来给出答案。Download chromedriver for you chrome version并安装selenium pip install -U selenium

import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import csv
browser = webdriver.Chrome(executable_path='/path/to/chromedriver')
browser.get("https://www.quora.com/topic/Graduate-Record-Examination-GRE-1")
time.sleep(1)
elem = browser.find_element_by_tag_name("body")
no_of_pagedowns = 5
while no_of_pagedowns:
    elem.send_keys(Keys.PAGE_DOWN)
    time.sleep(0.2)
    no_of_pagedowns-=1
post_elems =browser.find_elements_by_xpath("//a[@class='question_link']")
for post in post_elems:
    print(post.get_attribute("href"))

如果您使用的是windows-executable_path='/path/to/chromedriver.exe'

更改此变量no_of_pagedowns = 5以指定要向下滚动的次数。在

我得到以下输出

enter image description here

相关问题 更多 >