Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/83.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
Html 在搜索栏中输入值并从网页下载输出_Html_Web Scraping - Fatal编程技术网

Html 在搜索栏中输入值并从网页下载输出

Html 在搜索栏中输入值并从网页下载输出,html,web-scraping,Html,Web Scraping,我正在尝试搜索网页()。我认为相关的html源代码如下: <input name="txtStreetName" type="text" id="txtStreetName"> 这不起作用,只给了我下载的默认网页(即Jefferson未在搜索栏中输入并检索到)。我猜您对“requests.post”的引用与python的requests模块有关 由于您尚未指定要从搜索结果中提取的内容,我将简单地为您提供一个片段,以获取给定搜索查询的html: import requests qu

我正在尝试搜索网页()。我认为相关的html源代码如下:

<input name="txtStreetName" type="text" id="txtStreetName">

这不起作用,只给了我下载的默认网页(即Jefferson未在搜索栏中输入并检索到)。我猜您对“requests.post”的引用与python的requests模块有关

由于您尚未指定要从搜索结果中提取的内容,我将简单地为您提供一个片段,以获取给定搜索查询的html:

import requests

query = 'Jefferson'

url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
post_data = {'txtStreetName': query}

html_result =  requests.post(url, data=post_data).content

print html_result
如果您需要进一步处理html文件以提取一些数据,我建议您使用该模块进行处理

更新版本:

    #!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import csv
from string import ascii_lowercase
import codecs
import os.path
import time

def get_post_data(html_soup, query):
    view_state = html_soup.find('input', {'name': '__VIEWSTATE'})['value']
    event_validation = html_soup.find('input', {'name': '__EVENTVALIDATION'})['value']
    textbox1 = ''
    btn_search = 'Find'
    return {'__VIEWSTATE': view_state,
            '__EVENTVALIDATION': event_validation,
            'Textbox1': '',
            'txtStreetName': query,
            'btnSearch': btn_search
            }

arrayofstreets = ['Jefferson']


url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
html = requests.get(url).content
for each in arrayofstreets:
        payload = get_post_data(BeautifulSoup(html, 'lxml'), each)
        r = requests.post(url, data=payload).content
        outfile = "raw/" + each + ".html"
        with open(outfile, "w") as code:
            code.write(r)
            time.sleep(2)
我/你的第一个版本中的问题是我们没有发布所有必需的参数。要了解你需要发送什么,请在浏览器中打开网络监视器(Firefox中的Ctrl+Shitf+Q)如果你在网络日志中选择POST请求,你会在右边看到“参数选项卡”,在那里你的浏览器发送了POST参数


我猜您对“requests.post”的引用与python的requests模块有关

由于您尚未指定要从搜索结果中提取的内容,我将简单地为您提供一个片段,以获取给定搜索查询的html:

import requests

query = 'Jefferson'

url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
post_data = {'txtStreetName': query}

html_result =  requests.post(url, data=post_data).content

print html_result
如果您需要进一步处理html文件以提取一些数据,我建议您使用该模块进行处理

更新版本:

    #!/usr/bin/python
import requests
from bs4 import BeautifulSoup
import csv
from string import ascii_lowercase
import codecs
import os.path
import time

def get_post_data(html_soup, query):
    view_state = html_soup.find('input', {'name': '__VIEWSTATE'})['value']
    event_validation = html_soup.find('input', {'name': '__EVENTVALIDATION'})['value']
    textbox1 = ''
    btn_search = 'Find'
    return {'__VIEWSTATE': view_state,
            '__EVENTVALIDATION': event_validation,
            'Textbox1': '',
            'txtStreetName': query,
            'btnSearch': btn_search
            }

arrayofstreets = ['Jefferson']


url = 'http://www.phillyhistory.org/historicstreets/default.aspx'
html = requests.get(url).content
for each in arrayofstreets:
        payload = get_post_data(BeautifulSoup(html, 'lxml'), each)
        r = requests.post(url, data=payload).content
        outfile = "raw/" + each + ".html"
        with open(outfile, "w") as code:
            code.write(r)
            time.sleep(2)
我/你的第一个版本中的问题是我们没有发布所有必需的参数。要了解你需要发送什么,请在浏览器中打开网络监视器(Firefox中的Ctrl+Shitf+Q)如果你在网络日志中选择POST请求,你会在右边看到“参数选项卡”,在那里你的浏览器发送了POST参数


Hi Dziugas,这正是我所尝试的。我没有得到正确的输出。我在问题中编辑了上面的回答Hi Dziugas,这正是我所尝试的。我没有得到正确的输出。我在问题中编辑了上面的回答