2
<TABLE WIDTH="100%"> <TR> <TH scope="row" VALIGN="TOP" ALIGN="LEFT" WIDTH="10%">Inventors:</TH> <TD ALIGN="LEFT" WIDTH="90%">
 <B>Shimada; Masahiro</B> (Shiga, <B>JP</B>) </TD> </TR>
<TR><TH scope="row" VALIGN="TOP" ALIGN="LEFT" WIDTH="10%">Applicant: </TH><TD ALIGN="LEFT" WIDTH="90%"> <TABLE> <TR> <TH scope="column" ALIGN="center">Name</TH> <TH scope="column" ALIGN="center">City</TH> <TH scope="column" ALIGN="center">State</TH> <TH
scope="column" ALIGN="center">Country</TH> <TH scope="column" ALIGN="center">Type</TH> </TR> <TR> <TD> <B><br>Shimada; Masahiro</B> </TD><TD> <br>Shiga </TD><TD ALIGN="center"> <br>N/A </TD><TD ALIGN="center"> <br>JP </TD> </TD><TD ALIGN="left"> </TD>
</TR> </TABLE> </TD></TR>
<TR> <TH scope="row" VALIGN="TOP" ALIGN="LEFT" WIDTH="10%">Assignee:</TH>
<TD ALIGN="LEFT" WIDTH="90%">

<B>Ishida Co., Ltd.</B>
 (Kyoto, 
<B>JP</B>)
<BR>

</TD>
</TR>
       <TR><TH scope="row" VALIGN="TOP" ALIGN="LEFT" WIDTH="10%" NOWRAP>Appl. No.:
       </TH><TD ALIGN="LEFT" WIDTH="90%">
       <B>12/791,478</B></TD></TR>
       <TR><TH scope="row" VALIGN="TOP" ALIGN="LEFT" WIDTH="10%">Filed:
       </TH><TD ALIGN="LEFT" WIDTH="90%">
       <B>June 1, 2010</B></TD></TR>
     </TABLE>

取自 这个美国专利局 URL

以上是我需要获取数据的 HTML 表。但是当我使用:

trtemp=souptemp.findAll('tr')
PattentInventors=trtemp[7].text.strip()
PattentCompany=trtemp[11].text.strip()
PattentFiledtime=trtemp[13].text.strip()

tr 索引 7,11,13 在所有页面上都不是恒定的。所以我改用这样的 re 模块:

souptemp.findAll(text=re.compile("Assi"))[0]

这是为了获取数据,Assignee: Ishida Co., Ltd. (Kyoto, JP) 但我无法获取 tr 列表的索引。我怎样才能得到正确的索引 Assignee: Ishida Co., Ltd. (Kyoto, JP) 谢谢!

4

1 回答 1

3
In [78]: anchor = soup.findAll(text=re.compile("Assi"))[0]
In [77]: ' '.join(anchor.find_next('td').stripped_strings)
Out[77]: u'Ishida Co., Ltd. (Kyoto, JP )'

import bs4 as bs
import urllib2
import re

url = 'http://patft.uspto.gov//netacgi/nph-Parser?Sect1=PTO2&Sect2=HITOFF&p=1&u=%2Fnetahtml%2FPTO%2Fsearch-bool.html&r=2&f=G&l=50&co1=AND&d=PTXT&s1=%22X+ray%22.ABTX.&s2=detect.ABTX.&OS=ABST/%22X+ray%22+AND+ABST/detect&RS=ABST/%22X+ray%22+AND+ABST/detect'
soup = bs.BeautifulSoup(urllib2.urlopen(url).read())

anchor = soup.findAll(text=re.compile("Assi"))[0]
assignee = ' '.join(anchor.find_next('td').stripped_strings)
print(assignee)

产量

Ishida Co., Ltd. (Kyoto, JP )
于 2013-03-29T12:06:09.337 回答