2012-06-16 35 views
5

Tôi có một file html như sau: "/ 2.811.457/làm theo page = 2 & gsid = 3_5bce9b871484d3af90c89f37"BeautifulSoup không thể tìm thấy href trong tập tin sử dụng biểu thức chính quy

<form action="/2811457/follow?gsid=3_5bce9b871484d3af90c89f37" method="post"> 
<div> 
<a href="/2811457/follow?page=2&amp;gsid=3_5bce9b871484d3af90c89f37">next_page</a> 
&nbsp;<input name="mp" type="hidden" value="3" /> 
<input type="text" name="page" size="2" style='-wap-input-format: "*N"' /> 
<input type="submit" value="jump" />&nbsp;1/3 
</div> 
</form> 

làm thế nào để trích xuất các href" ở next_page?

nó là một phần của html, tôi dự định để làm cho nó rõ ràng. Khi tôi sử dụng BeautifulSoup,

print soup.find('a',href=re.compile('follow?page')) 

nó trở lại Không, tại sao? Tôi mới đến beautifulsoup, và tôi đã xem tài liệu, nhưng vẫn còn bối rối.

bây giờ tôi sử dụng một cách xấu xí:

urls = soup.findAll('a',href=True)) 
    for url in urls: 
     if follow?page in url: 
      print url 

Tôi cần một cách rõ ràng hơn và thanh lịch.

Trả lời

14

Bạn cần thoát khỏi dấu chấm hỏi. Cụm từ thông dụng w? có nghĩa là zero or one w. Hãy thử điều này:

print soup.find('a', href = re.compile(r'.*follow\?page.*')) 
+0

đánh dấu, bạn sẽ cho tôi biết cách trích xuất "1/3" từ tệp html? – young001

Các vấn đề liên quan