【问题标题】:Scraping dynamic website to get elements in <script tag> using BeautifulSoup and Selenium使用 BeautifulSoup 和 Selenium 抓取动态网站以获取 <script tag> 中的元素
【发布时间】:2016-10-13 09:15:53
【问题描述】:

我正在尝试使用 beautifulsoup 和 selenium 来抓取动态网站。我要过滤并放入 CSV 的属性包含在 &lt;script&gt; 标记中。我想提取包含在

脚本: 窗口.IS24 = 窗口.IS24 || {}; IS24.ssoAppName = "搜索"; IS24.applicationContext = "/Suche/error-reporter"; IS24.ab = {}; IS24.feature = {“SEARCH_BY_TELEKOM_SPEED_ENABLED”:真, IS24.resultList = { angularDebugInfoEnabled:假, navigationBarUrl: "/Suche/S-T/Haus-Kauf",

  nextPage: "/Suche/S-T/P-2/Haus-Kauf?pagerReporting=true",

  searchUrl: "/Haus-Kauf",
  isMobile: false,
  isTablet: false,
  query:     
{"realEstateType":"HOUSE_BUY","otpEnabled":true,"sortingCode":0,"location":      
{"isGeoHierarchySearch":true,
Schulze","referrer":["RESULT_LIST_GROUPED"],"**attributes":[  
{"title":"Kaufpreis","value":"249.012,75 €"}, 
{"title":"Wohnfläche","value":"129,87 m²"},{"title":"Zimmer","value":"4"},
{"title":"Grundstück","value":"400 m²"}],"checkedAttributes":["Gäste-**

我不确定如何将最后的属性提取到 CSV。你能帮我写代码吗?

【问题讨论】:

  • BS 使用 HTML,&lt;script&gt; 中的所有内容都不是 HTML 而是 JavaScript,所以对于 BS 来说它只是一个字符串。你必须使用标准的字符串方法或正则表达式来获取它(也许 JSON 来转换成 Python 字典)。
  • 可能显示完整的&lt;script&gt;。也许我们可以找到像data = json.loads(substring_from_script_tag)这样的方法
  • 你好,我将如何使用正则表达式?谢谢

标签: jquery python selenium web-scraping beautifulsoup


【解决方案1】:

下面是如何使用 beautifulSoup 从标签中提取属性值。

import urllib2
from bs4 import BeautifulSoup

req = urllib2.Request('http://website_to_grab_things_from.com')
response = urllib2.urlopen(req)
html = response.read()
soup = BeautifulSoup(html, "html.parser")
alltext = soup.getText()

#soup.findAll('TAGNAME', {'ATTR_NAME' :'ATTR_VALUE'})
result = soup.findAll('div', {'class' :'teaser-text'})

【讨论】:

  • 您好,谢谢您的回答。我已经尝试过了,但它不起作用。我不认为代码中实际上有一个类?下面是我正在查看的整个代码,其中包含我想以红色提取的信息:
  • IS24.resultList = { angularDebugInfoEnabled: false, navigationBarUrl: "/Suche/S-T/Haus-Kauf", nextPage: "/Suche/S-T/P-2/Haus-Kauf?pagerReporting=true" , searchUrl: "/Haus-Kauf", isMobile: false, isTablet: false, query: {"realEstateType":"HOUSE_BUY","otpEnabled":true,"sortingCode":0,"location": [..]
  • "referrer":["RESULT_LIST_LISTING"],"attributes":[{"title":"Kaufpreis","value":"625.000 €"},{"title":"Wohnfläche" ,"value":"216 m²"},{"title":"Zimmer","value":"7"}
  • @Nadja 你应该在很久以前添加此信息。
猜你喜欢
  • 2019-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
  • 2020-06-21
  • 1970-01-01
  • 2018-05-31
相关资源
最近更新 更多