Python 3.x 如何知道正在运行什么查询来获取网站中的数据以及如何提取数据

Python 3.x 如何知道正在运行什么查询来获取网站中的数据以及如何提取数据,python-3.x,web-scraping,Python 3.x,Web Scraping,我正在尝试获取网站上提供的日平均温度。但我没有得到任何价值,或者如果我只是复制粘贴数据,它会显示“没有数据记录”,而不是该网站上的表格。我做错了什么? 我正在使用以下代码 import pandas as pd import requests from bs4 import BeautifulSoup from tabulate import tabulate headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win

我正在尝试获取网站上提供的日平均温度。但我没有得到任何价值,或者如果我只是复制粘贴数据,它会显示“没有数据记录”,而不是该网站上的表格。我做错了什么? 我正在使用以下代码

import pandas as pd
import requests
from bs4 import BeautifulSoup
from tabulate import tabulate
headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36',
    }
r = requests.get('https://www.wunderground.com/',headers=headers)
res = requests.get("https://www.wunderground.com/history/daily/pk/karachi/OPKC/date/2017-1-3")
import urllib.request
soup = BeautifulSoup(res.content,'lxml')
tables = soup.find_all('table')
for table in tables:
    df = pd.read_html(str(table))
    print( tabulate(df[0], headers='keys', tablefmt='psql') )
print(soup.get_text())

我用请求检查了响应,在r.content中没有发现表标记。不要从页面中读取,而是考虑使用下面的方法来使用API。 通过将参数传递到get方法来获得json响应。然后通过迭代json响应的每个对象来获得所需的内容

import requests
import csv

base_url = "https://api.weather.com/v1/geocode/24.90138817/67.15000153/observations/historical.json?"

data = {
  "apiKey": "6532d6454b8aa370768e63d6ba5a832e",
  "startDate": "20170103",
  "endDate": "20170103",
  "units": "e"
}

r = requests.get(base_url, params=data)
d = r.json()

headers = ['timestamp', 'temp', 'precip/hr', 'windspeed']
with open('results.csv', 'a') as f:
  writer = csv.writer(f)
  writer.writerow(headers)
  for item in d['observations']:
    writer.writerow([item['expire_time_gmt'], item['temp'],item['precip_hrly'], item['wspd']])
f.close()
下面的照片

timestamp,temp,preciptation/hr,windspeed
1483392300,63,,7
1483394100,61,,3
1483395900,61,,3
1483397700,59,,7
1483399500,59,,6
1483401300,59,,5
1483403100,57,,7
1483404900,57,,5
1483406700,57,,5
1483408500,57,,9
1483413900,55,,7
1483415700,55,,7
1483417500,55,,6
1483419300,55,,3
1483421100,57,,3
1483422900,61,,3
1483424700,64,,5
1483426500,66,,7
1483428300,66,,5
1483430100,72,,5
1483431900,73,,0
1483433700,75,,2

右键单击页面上的任意位置,选中Inspect元素,转到Networks选项卡,检查正在发出的请求和正在接收的响应。此外,还可以查看Selenium。我这样做时没有看到“table”标记:for tag in soup.find_all(True):print(tag.name)您应该使用python对web抓取动态内容进行一些研究,因为目标站点上的表正在生成。是的,我正在尝试学习代码。实际上,我对编写和学习python 3还不熟悉。但要从网站上获取数据,您还需要了解HTML、SQLJSON等其他信息。对此我一点也不知道。嗨,祝你好运。。。谢谢你的回答,并引导我进入一个新的领域。谢谢!祝你有一个幸运的日子:)如果答案是你想要的,请给我一些时间去投票:)当我投票时,系统会说“谢谢你的反馈!那些声誉低于15的人所投的票会被记录下来,但不要改变公开显示的帖子分数。”对不起!!!