Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/71.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中的Urllib2:为什么它不返回网页格式而不是实际数据_Python_Html_Web Scraping_Urllib2 - Fatal编程技术网

python中的Urllib2:为什么它不返回网页格式而不是实际数据

python中的Urllib2:为什么它不返回网页格式而不是实际数据,python,html,web-scraping,urllib2,Python,Html,Web Scraping,Urllib2,有人能告诉我为什么,当我运行此代码时: import urllib2 for i in range(1,2): id_name ='AP' + str("{:05d}".format(i)) web_page = "http://aps.unmc.edu/AP/database/query_output.php?ID=" + id_name page = urllib2.urlopen(web_page) html = page.r

有人能告诉我为什么,当我运行此代码时:

import urllib2
for i in range(1,2):
        id_name ='AP' + str("{:05d}".format(i))
        web_page = "http://aps.unmc.edu/AP/database/query_output.php?ID=" + id_name
        page = urllib2.urlopen(web_page)
        html = page.read()
        print html
它返回:

<html>
<head>
<title>detailed information</title>
<style type="text/css">
H1 {font-family:"Time New Roman", Times; font-style:bold; font-size:18pt; color:blue}
H1{text-align:center}
P{font-family:"Time New Roman", Times; font-style:bold; font-size:14pt; line-height:20pt}
P{text-align:justify;margin-left:0px; margin-right:0px;color:blue}
/body{background-image:url('sky.gif')}
/
A:link{color:blue}
A:visited{color:#996666}
</style>
</head>
<H1>Antimicrobial Peptide APAP00001</H1>
<html>
<p style="margin-left: 400px; margin-top: 4; margin-bottom: 0; line-height:100%">
<b>
<a href = "#" onclick = "window.close(self)"><font size="3" color=blue>Close this window
</font> </a>
</b>
</p>
</p>
</body>
</html>
然而,我似乎仍然有相同的答案(例如,如果我运行edit 1代码并直接输出到一个文件,然后在输出文件中grep肽序列,它就不存在了)。

使用请求库:

import requests
from bs4 import BeautifulSoup
data_require = ["Net charge", ]
for i in range(1,2):
    id_value ="{:05d}".format(i)
    url = "http://aps.unmc.edu/AP/database/query_output.php"
    payload = {"ID": id_value}
    response = requests.get(url, params=payload)
    soup = BeautifulSoup(response.content, 'html.parser')
    table_structure = soup.find('table')
    all_p_tag = table_structure.find_all('p')
    data = {}
    for i in range(0, len(all_p_tag), 2):
        data[all_p_tag[i].text] = all_p_tag[i+1].text.encode('utf-8').strip()
        print("{} {}".format(all_p_tag[i].text, all_p_tag[i+1].text.encode('utf-8').strip()))
    print(data)
注: 您不需要将
“{:05d}”.format(i)
转换为字符串,因为使用
format()
时它只返回字符串,因为它是字符串格式

此外,我还更新了代码,以获取标记详细信息。
您不需要使用grep,因为BeautifulSoup已经提供了此类功能。

在原始代码段中,您使用“AP00001”作为查询参数:

id_name ='AP' + str("{:05d}".format(i))
因此,您的url是:“”,而不是“”

使用
请求的第一个代码段的固定版本

url = "http://aps.unmc.edu/AP/database/query_output.php"
for i in range(1,2):
    id_name = "{:05d}".format(i)
    response = requests.get(url, params={"ID":id_name})
    print response.content

我很感激你的帮助,但这对我来说并不管用。我如前所述更改了代码(我在上述问题中添加了代码),但是,如前所述,我仍然看不到所需的信息(例如,当我从建议的代码中grep输出文件中的肽序列时,肽序列不在那里?)。不过,感谢您的帮助,我非常感谢。虽然使用
请求
而不是urllib2肯定是一个很好的建议(因为它使生活更轻松),但它本身并不能解决传递错误的“id”参数的问题。此外,如果使用
请求
,至少要学会正确使用它(将查询参数作为dict传递,而不是手动生成查询字符串:
请求。get(“http://aps.unmc.edu/AP/database/query_output.php,参数={“ID”:“00001”})
如果没有多个参数,并且请求方法是GET,我不介意手动构建它,因为如果参数存在,那么它也将在同一阶段进行处理,最后它们将被连接。@E.Dohrty和@brunodesshuilliers;检查代码更新这非常有帮助,非常感谢您的时间。您为什么要更改url排名第二?
url = "http://aps.unmc.edu/AP/database/query_output.php"
for i in range(1,2):
    id_name = "{:05d}".format(i)
    response = requests.get(url, params={"ID":id_name})
    print response.content