Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/87.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从具有多个“选择”字段的网站中进行刮取?_Python_Html_Web Scraping - Fatal编程技术网

Python 如何从具有多个“选择”字段的网站中进行刮取?

Python 如何从具有多个“选择”字段的网站中进行刮取?,python,html,web-scraping,Python,Html,Web Scraping,我希望从以下几个方面了解整个2018年12月的天气情况: 此网页有2个选择字段。我完全不熟悉html和post请求。我已经阅读了的答案。在我看来,我需要包括所有字段id值对。下面是我的代码 import requests r = requests.post( "https://www.timeanddate.com/weather/usa/new-york/historic?month=12&year=2018", data={ "month": r'20

我希望从以下几个方面了解整个2018年12月的天气情况:

此网页有2个选择字段。我完全不熟悉html和post请求。我已经阅读了的答案。在我看来,我需要包括所有字段id值对。下面是我的代码

import requests
r = requests.post(
    "https://www.timeanddate.com/weather/usa/new-york/historic?month=12&year=2018",
    data={
        "month": r'2018-12',
        "wt-his-select": r"20181205",
    })

我希望2018年12月5日的天气记录符合我在上面输入的id值对,但我总是让12月1日的天气拉出标签,因为数据是json格式的。然后将其读入字典以转换为数据帧:

import requests
from bs4 import BeautifulSoup
import json
import pandas as pd

r = requests.get("https://www.timeanddate.com/weather/usa/new-york/historic?month=12&year=2018")

soup = BeautifulSoup(r.text, 'html.parser')
scripts = soup.find_all('script')
for script in scripts:
    if 'var data=' in script.text:
        jsonStr = script.text
        jsonStr = jsonStr.split('var data=')[-1].split(';window.')[0]

        jsonData = json.loads(jsonStr)

weather = jsonData['detail']
results = pd.DataFrame()
for each in weather:
    results = results.append(pd.DataFrame([each]), sort=True).reset_index(drop=True)
输出:

其他:

通过访问json,您可以一小时一小时地获取各个天。只需更改有效负载中的参数即可获得特定日期:

import pandas as pd

url = 'https://www.timeanddate.com/scripts/cityajax.php'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

year = 2018
month = 12
day = 1

payload = {
'n': 'usa/new-york',
'mode': 'historic',
'hd': '%d%02d%02d' %(year, month, day),
'month': '%02d' %(month),
'year': '%d' %(year)}

data = requests.get(url, headers=headers, params=payload).text
table = pd.read_html('<table>' + data + '</table>')[0][:-1]
table = table.dropna(axis=1)

我们使用beautifulsoup拉出标记,因为数据是json格式的。然后将其读入字典以转换为数据帧:

import requests
from bs4 import BeautifulSoup
import json
import pandas as pd

r = requests.get("https://www.timeanddate.com/weather/usa/new-york/historic?month=12&year=2018")

soup = BeautifulSoup(r.text, 'html.parser')
scripts = soup.find_all('script')
for script in scripts:
    if 'var data=' in script.text:
        jsonStr = script.text
        jsonStr = jsonStr.split('var data=')[-1].split(';window.')[0]

        jsonData = json.loads(jsonStr)

weather = jsonData['detail']
results = pd.DataFrame()
for each in weather:
    results = results.append(pd.DataFrame([each]), sort=True).reset_index(drop=True)
输出:

其他:

通过访问json,您可以一小时一小时地获取各个天。只需更改有效负载中的参数即可获得特定日期:

import pandas as pd

url = 'https://www.timeanddate.com/scripts/cityajax.php'
headers = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36'}

year = 2018
month = 12
day = 1

payload = {
'n': 'usa/new-york',
'mode': 'historic',
'hd': '%d%02d%02d' %(year, month, day),
'month': '%02d' %(month),
'year': '%d' %(year)}

data = requests.get(url, headers=headers, params=payload).text
table = pd.read_html('<table>' + data + '</table>')[0][:-1]
table = table.dropna(axis=1)

请看一看python漂亮的用于提取数据的汤库。谢谢你的回答。你能再具体一点吗?我在bs4文档中找不到相关的方法或函数。请看一下python漂亮的用于提取数据的汤库。谢谢你的回答。你能再具体一点吗?我在bs4文档中找不到相关的方法或函数。这是一个很好的答案!我真的很感激你的帮助。我还注意到,有两个表对应于该网页上的两个下拉列表。您检索到的json数据用于第一个表,即每6小时记录一次的表。但是,我仍然找不到第二个表的数据,即小时记录表。您将如何检索该文件的数据?谢谢。我现在无法查看,但稍后会查看并返回给您。@同构,检查更新的解决方案。也要确保接受上述解决方案。这是一个很好的答案!我真的很感激你的帮助。我还注意到,有两个表对应于该网页上的两个下拉列表。您检索到的json数据用于第一个表,即每6小时记录一次的表。但是,我仍然找不到第二个表的数据,即小时记录表。您将如何检索该文件的数据?谢谢。我现在无法查看,但稍后会查看并返回给您。@同构,检查更新的解决方案。还要确保接受上述解决方案。