我正在用python抓取一个网站http://i.cantonfair.org.cn/en/expexhibitorlist.aspx?categoryno=411。 我想在一个div标记中得到一个链接,其中有两个a标记,如:
<div id="main_category">
<div class="tit1"><a href="#" onclick="ExpandStage(1);"><strong>Phase 1</strong><br />April 15 - 19</a></div>
<ul id="phase1">
<li><a href="expexhibitorlist.aspx?categoryno=411">Consumer Electronics and Information Products</a></li>
<li><a href="expexhibitorlist.aspx?categoryno=412">Electronic and Electrical Products</a></li>
我只想要像这样的标签
<a href="expexhibitorlist.aspx?categoryno=411">Consumer Electronics and Information Products</a>
。另外,如何使用regex查找这些URL?你知道吗
我正在这样努力
from bs4 import BeautifulSoup
import re
import urllib.request
r = urllib.request.urlopen('http://i.cantonfair.org.cn/en/expexhibitorlist.aspx?categoryno=410').read()
soup = BeautifulSoup(r, "html.parser")
letters = soup.find_all("div",{"id":"main_category"})
for element in letters:
categories = element.a.get_text()
print (categories)
我使用的是Python2.7,下面的代码对我很有用。python3可以使用相同的方法。希望能有帮助:
相关问题 更多 >
编程相关推荐