的Python POST请求
问题描述:
我为使用Python请求搜索以下网站:https://www.investing.com/的术语“耐用品订单美国”的Python POST请求
我检查在检查面板的“网络”选项卡,现在看来,这是根本“quotes_search_text”:用以下形式完成的“耐用品订单美国”
所以,我试图使用python:
URL = 'https://www.investing.com/'
data = {'quotes_search_text':'Durable Goods Orders US'}
resp = requests.post(URL, data=data, headers={ 'User-Agent': 'Mozilla/5.0', 'X-Requested-With': 'XMLHttpRequest'})
然而,这并不返回结果,而做手工,我可以看到。 所有搜索结果中应该有“GS-标题”作为类属性(按照页面检查),但是当我做的:
soup = BeautifulSoup(resp.text, 'html.parser')
soup.select(".gs-title")
我看不出有什么结果...... 有POST请求的某些方面我没有考虑到? (这里是一个完整的菜鸟)
答
在聊天中详细地讲过这个之后,有很多变化。为了检索您要查找的信息,您需要运行正在运行的JS。您可以将query
变量更改为任何您想要的。
import requests
import json
from urllib.parse import quote_plus
URL = 'https://www.googleapis.com/customsearch/v1element'
query = 'Durable Goods Orders US'
query_formatted = quote_plus(query)
data = {
'key':'AIzaSyCVAXiUzRYsML1Pv6RwSG1gunmMikTzQqY',
'num':10,
'hl':'en',
'prettyPrint':'true',
'source':'gcsc',
'gss':'.com',
'cx':'015447872197439536574:fy9sb1kxnp8',
'q':query_formatted,
'googlehost':'www.google.com'
}
headers = {
'User-Agent':'Mozilla/5.0',
'Referer':'https://www.investing.com/search?q=' + query_formatted,
}
resp = requests.get(URL, params=data, headers=headers)
j = json.loads(resp.text)
# print(resp.text)
for r in j['results']:
print(r['title'], r['url'])
我相信你的'find_all'选择器正在寻找一个类属性,当它期待一个HTML标记。 –
@double_j不,我正在寻找一个类属性...在这里什么目标元素看起来像:'United States Durable Goods Orders MoM' –
没关系,但BeautifulSoup永远不会找到你现在的方式标签。你应该这样写:'soup.find_all('a',{'class':'gs-title'})' –