【问题标题】:For, html, and beautiful soupFor,html,还有美汤
【发布时间】:2016-11-04 04:14:26
【问题描述】:

我正在尝试做类似的事情:

from BeautifulSoup import BeautifulSoup
import urllib2,unicodedata
import re

for x in range(1,105):

html_page = urllib2.urlopen('http://xxxxxx/BUSCAR/H=1;OR=5;ST=;LIST_ART_PAGENUMBER='+str(x)+';/Dxxxxx.aspx')
soup = BeautifulSoup(html_page)
for link in soup.findAll('a', attrs={'href': re.compile("^http://xxxxxx/PRODUCTO/PROD_ID")}):
    print link.get('href')

提取链接。我正确地提取了链接。但我想提取1到105的范围

但这不起作用!

error: expected an indented block

【问题讨论】:

    标签: python html hyperlink beautifulsoup


    【解决方案1】:

    开始 for 循环时需要缩进。试试这个:

    from BeautifulSoup import BeautifulSoup
    import urllib2,unicodedata
    import re
    
    for x in range(1,105):
    
        html_page = urllib2.urlopen('http://xxxxxx/BUSCAR/H=1;OR=5;ST=;LIST_ART_PAGENUMBER='+str(x)+';/Dxxxxx.aspx')
        soup = BeautifulSoup(html_page)
        for link in soup.findAll('a', attrs={'href':re.compile("^http://xxxxxx/PRODUCTO/PROD_ID")}):
             print link.get('href')
    

    【讨论】:

    • 嗨@DamianPerez,不客气。这是非常基本的,没什么。保持学习!如果您觉得满意,您可以接受答案。
    猜你喜欢
    • 2011-04-20
    • 2012-12-19
    • 2015-03-27
    • 2012-10-05
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多