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>
而不是页面上的实际数据(http://aps.unmc.edu/AP/database/query_output.php?ID=00001)(例如净费用,长度)?
如果我稍微以某种方式编辑此代码,是否可以返回页面上的所有信息(例如有关净费用,长度等的信息),而不仅仅是有关页面格式化方式的信息?
由于
编辑1:由于Gahan的评论如下,我试过这个: 导入请求 来自bs4 import BeautifulSoup
for i in range(8,9):
webpage = "https://dbaasp.org/peptide-card?type=39&id=" + str(i)
response = requests.get(webpage)
soup = BeautifulSoup(response.content, 'html.parser')
print soup
但是,我似乎仍然是相同的答案(例如,如果我运行编辑1代码并将输出直接输出到文件,然后在输出文件中grep肽序列,则不存在)。
答案 0 :(得分:2)
在原始代码段中,使用“AP00001”作为查询参数:
id_name ='AP' + str("{:05d}".format(i))
所以您的网址是:“http://aps.unmc.edu/AP/database/query_output.php?ID=AP00001”,而不是“http://aps.unmc.edu/AP/database/query_output.php?ID=00001”
使用requests
的第一个代码段的固定版本:
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
答案 1 :(得分:-1)
使用请求库:
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已经提供了这样的设施。