Python 搜索特定值的字符串=
我试图使用请求库在html响应中搜索特定值Python 搜索特定值的字符串=,python,dictionary,Python,Dictionary,我试图使用请求库在html响应中搜索特定值 import requests while True: url = 'https://www.example.com/' page = requests.get(url, allow_redirects=True, verify=False) var = page.content 该值看起来像一个字典,但我无法使用:var=dict(page.content)将整个response.content转换为dict,因为它会给出错误“dic
import requests
while True:
url = 'https://www.example.com/'
page = requests.get(url, allow_redirects=True, verify=False)
var = page.content
该值看起来像一个字典,但我无法使用:var=dict(page.content)将整个response.content转换为dict,因为它会给出错误“dictionary update sequence元素#0的长度为1;需要2”
我已尝试使用以下搜索方法:
searchObj = re.search( r'(.*)id="X" value=(.*?) .*', var, re.M)
if searchObj:
print "search --> searchObj.group() : ", searchObj.group()
但这不是我要寻找的-最终目标是在从网站请求返回的内容中找到一个特定的值,它在内容中看起来是这样的:
-需要提取的数据为value=“12345”,或者更具体地说是12345
提前感谢不要为此使用正则表达式,请使用为此而创建的库,例如:
将bs4作为bs导入
导入请求
resp=requests.get('http://www.google.com')
汤=bs.BeautifulSoup(分别为文本)
element=soup.find(attrs={'id':'hplogo})#将搜索'google'徽标
打印元素
>> ׳™׳©׳¨׳�׳�
这应该适合您:
import re
import requests
VALUE_RGX = re.compile(r'id="X" value="([A-Za-z0-9_\-]+)"')
url = 'https://www.example.com/'
page = requests.get(url, allow_redirects=True, verify=False)
matched_groups = VALUE_RGX.match(page.text)
if len(matched_groups) > 1:
print("Found Value: {}".format(matched_groups[1]))
else:
print("Did not find value..")
最好使用专门的库解析XML和HTML,但如果是一次性操作且输出是可预测的,则可以使用正则表达式。以下re应该可以工作
r'id=\"test\"\svalue=\"(.*?)\"'
开始时的
(.*)
捕获了所有内容。使用beautifulsoup,找到标签并提取属性我真的希望你拥有这个网站或与所有者交谈过,因为如果你不知道自己在做什么,并且你正在无限循环地访问该页面,“有人会很有理由生你的气的。”两位炼金术士虽然你说得对,他不应该在没有超时的情况下用无限循环刮一页,但这真的没关系。大多数现代网站都是用Apache或Nginx构建的,如果在短时间内尝试了太多连接,他们会关闭他的连接。@nivixzixer和我运行了一个反病毒程序,但如果你试图感染我的机器,我仍然会有理由生气。这是我最喜欢的网站帖子之一!这可以打印整个元素-但我只查找1 value=”“etc-只打印我要分析的部分效果很好,但我需要进一步细化它。Ty so Faerror“如果len(匹配的组>0):类型错误:类型为'bool'的对象没有len()Oops,有一个输入错误。应该是len(匹配的组)>1
。我将更新postsearchObj=re.search(r'id=\'X\'\svalue=\'(.*?\”),var,re.M;re.I)如果searchObj:print searchObj.group(1)其他:print“未找到任何内容!”!!“利用你的评论,我可以开始从回复中提取我所需要的东西——感谢你
r'id=\"test\"\svalue=\"(.*?)\"'