【发布时间】:2018-09-17 12:53:59
【问题描述】:
我用 python 编写了一个脚本来从网站上获取课程资料列表。要显示课程材料,有必要填写一些inputs,如果您从其登录页面跟踪Find Textbooks 链接,如图一所示。
但是,当您相应地填写输入内容时,课程资料就会显示出来(我的做法如图二所示)。
看来,我以正确的方式完成了所有操作,但无法获取项目。当我执行我的脚本时,它不会解析任何内容,也不会引发任何错误。我在脚本中使用的选择器应该是准确的。
这是我目前的尝试:
import requests
from bs4 import BeautifulSoup
url = "https://uncg.bncollege.com/webapp/wcs/stores/servlet/BNCBTBListView"
payload = {
'storeId':'19069',
'catalogId':'10001',
'langId':'-1',
'clearAll':'',
'viewName':'TBWizardView',
'secCatList':'',
'removeSectionId':'',
'mcEnabled':'N',
'showCampus':False,
'selectTerm':'Select Term',
'selectDepartment':'Select Department',
'selectSection':'Select Section',
'selectCourse':'Select Course',
'campus1':'17548065',
'firstTermName_17548065':'Fall 2018',
'firstTermId_17548065':'84599238',
'section_1': '85441456',
'section_2':'',
'section_3':'',
'section_4':'',
'numberOfCourseAlready':'4'
}
with requests.Session() as s:
s.headers={"User-Agent":"Mozilla/5.0"}
res = s.post(url,data=payload)
soup = BeautifulSoup(res.text,"lxml")
for items in soup.select("#skipNavigationToThisElement a"):
print(items.text)
任何解决问题的帮助将不胜感激。
图片二
【问题讨论】:
-
您正在访问的站点正在使用 javascript 生成一些 HTML 客户端。实际上必须执行 javascript 才能查看内容。您可以使用 selenium 之类的工具。
-
如前所述,您需要可以处理 JavaScript 的工具...如果您使用
print(soup),您将在源代码中看到<noscript>Please enable JavaScript to view the page content.</noscript>,而不是所需的源...您可以尝试相同的方法requests-HTML ,但还不太稳定 -
是否可以使用 selenium 取回我使用请求 @sir Andersson 中断的链接?我的意思是,使用
driver.get(res.url)从最后一行启动 webdriver -
我猜你可以尝试将 cookie 从
requests会话传递到 Selenium 会话(也许做更多的事情),但它看起来比纯 Selenium 解决方案更复杂
标签: python python-3.x post web-scraping beautifulsoup