我正在尝试访问
中的元素<script type="text/javascript">ReportPopper("http://asd.asd.asd/ReportOutput/asd-asd-41cc-asd-asd.xls");<script>
使用beautifulsoup
,很遗憾,我不确定如何访问ReportPopper部件并将其分配给使用Python
对不起,如果已经回答。 我试过在find('ReportPopper')中添加ReportPopper并给我一个none元素。
import requests
import io
import os
from bs4 import BeautifulSoup
participation = requests.post(url=report_post_url,data=request_post_report_form,headers=report_post_headers,stream=True)
print(participation)
soup = BeautifulSoup(participation.text, 'html.parser')
for n in soup.find_all('script'):
javascript = n['ReportPopper']
print(javascript)
我希望得到的最终结果是:
javascript = "http://asd.asd.asd/ReportOutput/asd-asd-41cc-asd-asd.xls"
作为我的输出:
Traceback (most recent call last):
File "c:\Users\John asd\Documents\GitHub\asd.net\testing.py", line 184, in <module>
javascript = n['ReportPopper']
File "C:\Users\John asd\asd\Local\Programs\Python\Python37\lib\site-packages\bs4\element.py", line 1016, in __getitem__
return self.attrs[key]
KeyError: 'ReportPopper'
答案 0 :(得分:1)
re.compile()返回一个regular expression object,这意味着h是一个正则表达式对象。
regex对象具有自己的match方法,带有可选的pos和endpos参数:
regex.match(string[, pos[, endpos]])
from bs4 import BeautifulSoup
import re
html = """<script>ReportPopper("http://asd.asd.asd/ReportOutput/asd-asd-41cc-asd-asd.xls");</script>"""
soup = BeautifulSoup(html, 'lxml')
script = soup.find_all("script")
pattern = re.compile('ReportPopper(.*);')
for i in script:
strObj = i.text
match = pattern.search(strObj)
if match:
print(strObj.split("ReportPopper(")[1][:-2])
O / P:
"http://asd.asd.asd/ReportOutput/asd-asd-41cc-asd-asd.xls"
答案 1 :(得分:1)
对于bs4 4.7.1,您可以使用:contains响应中是否存在该字符串
from bs4 import BeautifulSoup as bs
# r = requests.get(url)
# html - r.content
html = '<script type="text/javascript">ReportPopper("http://asd.asd.asd/ReportOutput/asd-asd-41cc-asd-asd.xls");<script>'
soup = bs(html, 'lxml')
s = soup.select_one('script:contains(ReportPopper)').text
url = s.split('"')[1]
print(url)