如何使用Python 3提取某些html标签之间的文本?

时间:2019-11-21 21:25:07

标签: python web-scraping

我正在尝试抓取包含公司名称的网页。名称在标签之间。格式为:

<option value="15589" id="optExhibitor15589" title="N571  Company One, Inc">N571 Company One, Inc</option>
<option value="16441" id="optExhibitor16441" title="N873  Company Two">Company Two</option>
<option value="14863" id="optExhibitor14863" title="N219  Company Three">N219 Company Three</option>

我尝试使用.readline()将文件分成几行,但是我不知道如何提取title=">之间的文本。

我要提取数百个这样的名称,并且要生成公司名称列表。

1 个答案:

答案 0 :(得分:0)

您可以使用scrappy或其他库进行抓取,但是由于您已经获得了所需的东西。这可以帮助您获取值:

a = '<option value="15589" id="optExhibitor15589" title="N571  Company One, Inc">N571 Company One, Inc</option>'
beginning = a.find('title=') # Returns the integer at the location of 'title'
end = a.find('\">') # Returns the integer at the closing tag
print(a[beginning+6:end+1])

提供以下输出: "N571 Company One, Inc"

相关问题