gpt4 book ai didi

python - 如何识别并跟踪链接,然后使用 BeautifulSoup 从新网页打印数据

转载 作者:太空宇宙 更新时间:2023-11-03 19:41:39 24 4
gpt4 key购买 nike

我正在尝试 (1) 从网页抓取标题,(2) 打印标题,(3) 通过链接转到下一页,(4) 从下一页抓取标题,以及 (5)打印下一页的标题。

步骤(1)和(4)是相同的功能,步骤(2)和(5)是相同的功能。唯一的区别是函数 (4) 和 (5) 在下一页上执行。

#Imports
from urllib.request import urlopen
from bs4 import BeautifulSoup
import re


##Internet
#Link to webpage
web_page = urlopen("http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=31&f=G&l=50&co1=AND&d=PTXT&s1=(%22deep+learning%22.CLTX.+or+%22deep+learning%22.DCTX.)&OS=ACLM/%22deep+learning%22")
#Soup object
soup = BeautifulSoup(web_page, 'html.parser')

我对步骤 1 和 2 没有任何问题。我的代码能够获取标题并有效地打印它。步骤 1 和 2:

##Get Data
def get_title():
#Patent Number
Patent_Number = soup.title.text
print(Patent_Number)

get_title()

我得到的输出正是我想要的:

#Print Out
United States Patent: 10530579

我在执行步骤 3 时遇到问题。对于步骤 (3),我能够识别正确的链接,但无法跟随它进入下一页。我正在识别我想要的链接,即图像标签上方的“href”。

Picture of link to follow.

以下代码是我的步骤 3,4 和 5 的工作草案:

#Get
def get_link():
##Internet
#Link to webpage
html = urlopen("http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=31&f=G&l=50&co1=AND&d=PTXT&s1=(%22deep+learning%22.CLTX.+or+%22deep+learning%22.DCTX.)&OS=ACLM/%22deep+learning%22")
#Soup object
soup = BeautifulSoup(html, 'html.parser')
#Find image
##image = <img valign="MIDDLE" src="/netaicon/PTO/nextdoc.gif" border="0" alt="[NEXT_DOC]">
#image = soup.find("img", valign="MIDDLE")
image = soup.find("img", valign="MIDDLE", alt="[NEXT_DOC]")
#Get new link
new_link = link.attrs['href']
print(new_link)

get_link()

我得到的输出:

#Print Out
##/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=32&f=G&l=50&co1=AND&d=PTXT&s1=(%22deep+learning%22.CLTX.+or+%22deep+learning%22.DCTX.)&OS=ACLM/"deep+learning"

输出是我想要访问的确切链接。简而言之,我尝试编写的函数将打开 new_link 变量作为新网页,并在新网页上执行与 (1) 和 (2) 中执行的相同功能。结果输出将是两个标题,而不是一个(一个用于网页,一个用于新网页)。

本质上,我需要写一个:

urlopen(new_link)

函数,而不是:

print(new_link)

功能。然后,在新网页上执行步骤 4 和 5。但是,我无法弄清楚如何打开新页面并获取标题。一个问题是 new_link 不是一个 url,而是一个我想要点击的链接。

最佳答案

尽管您找到了解决方案,以防万一有人尝试类似。我的以下解决方案并不推荐用于所有情况。在这种情况下,因为所有页面的 url 仅页码不同。我们可以动态生成这些,然后批量请求,如下所示。您只需更改 r 的上限,直到页面存在即可工作。

from urllib.request import urlopen
from bs4 import BeautifulSoup
import pandas as pd

head = "http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=" # no trailing /
trail = """&f=G&l=50&co1=AND&d=PTXT&s1=("deep+learning".CLTX.+or+"deep+learning".DCTX.)&OS=ACLM/"deep+learning"""

final_url = []
news_data = []
for r in range(32,38): #change the upper range as per requirement
final_url.append(head + str(r) + trail)
for url in final_url:
try:
page = urlopen(url)
soup = BeautifulSoup(page, 'html.parser')
patentNumber = soup.title.text
news_articles = [{'page_url': url,
'patentNumber': patentNumber}
]
news_data.extend(news_articles)
except Exception as e:
print(e)
print("continuing....")
continue
df = pd.DataFrame(news_data)

关于python - 如何识别并跟踪链接,然后使用 BeautifulSoup 从新网页打印数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60390617/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com