我有一个python脚本,我遇到了麻烦,我想我已将问题缩小到这一行:
formkey = soup.find_all("script")[9].string[153:][:-141]
我相信此行应该在此页面上返回“form_key”的值https://shop.adidas.ae/en/forum-mis-wrap-shoes/BY4412.html
有什么我想念的吗?感谢
答案 0 :(得分:0)
如果您想要输入name="form_key"
做的:
form_key_value = soup.find('input', {'name':'form_key'})['value']
查看页面的来源,这个值是你想要的,并且更容易以这种方式获得它,而且更像Pythonic而不是解析JavaScript
答案 1 :(得分:0)
您应该使用BeautifulSoup查找元素,然后使用正则表达式(import re, requests
from bs4 import BeautifulSoup
regex = re.compile(r'formKey:\s*"(.*)"')
r = requests.get('https://shop.adidas.ae/en/forum-mis-wrap-shoes/BY4412.html')
soup = BeautifulSoup(r.text, "html5lib")
scripts = soup.find_all("script")
all_of_them = [None if match is None else match.groups() for match in [regex.search(script.text) for script in scripts]]
just_the_matches = [match.groups() for match in [regex.search(script.text) for script in scripts] if script is not None]
)来解析目标元素中的文本。像这样:
>>> all_of_them
[None, None, None, None, None, None, None, None, None, (u'U53YNK8paAE4Cdij',), None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
>>> just_the_matches
[(u'U53YNK8paAE4Cdij',)]
结果:
OutOfRangeError (see above for traceback): RandomShuffleQueue '_1_shuffle_batch/random_shuffle_queue' is closed and has insufficient elements (requested 3, current size 0)
[[Node: shuffle_batch = QueueDequeueManyV2[component_types=[DT_FLOAT], timeout_ms=-1, _device="/job:localhost/replica:0/task:0/cpu:0"](shuffle_batch/random_shuffle_queue, shuffle_batch/n)]]
重要:强>
同时强>