刮刀无法打印所有结果

时间:2017-08-28 11:23:37

标签: python python-3.x web-scraping web-crawler

我已经在python中写了一个脚本来刮掉" name"和"电话"来自craigslist的五件商品。我面临的问题是,当我运行我的脚本时,它只提供三个结果而不是五个结果。更具体地说,由于前两个链接在其页面中没有其他链接(联系信息),因此他们不需要再打开任何其他页面的请求。但是,如果ano_page_link:"在我的第二个函数中的声明,永远不会打印。如何解决这个缺陷,以便它是否有电话号码,刮刀将打印所有五个结果。

我尝试的脚本:

import re ; import requests ; from lxml import html

base = "http://bangalore.craigslist.co.in"

url_list = [
'http://bangalore.craigslist.co.in/reb/d/flat-is-for-sale-at-cooke-town/6266183606.html',
'http://bangalore.craigslist.co.in/reb/d/prestige-sunnyside/6259128505.html',
'http://bangalore.craigslist.co.in/reb/d/jayanagar-2nd-block-4000-sft/6221720477.html',
'http://bangalore.craigslist.co.in/reb/d/prestige-ozone-type-3-r-villa/6259928614.html',
'http://bangalore.craigslist.co.in/reb/d/zed-homes-3-bedroom-flat-for/6257075793.html'
]

def get_link(medium_link):
    response = requests.get(medium_link).text
    tree = html.fromstring(response)
    try:
        name = tree.cssselect('span#titletextonly')[0].text
    except IndexError:
        name = ""
    try:
        link = base + tree.cssselect('a.showcontact')[0].attrib['href']
    except IndexError:
        link = ""
    parse_doc(name, link)

def parse_doc(title, ano_page_link):

    if ano_page_link:
        page = requests.get(ano_page_link).text            
        tel = re.findall(r'\d{10}', page)[0] if re.findall(r'\d{10}', page) else ""
        print(title, tel)

if __name__ == '__main__':
    for link in url_list:
        get_link(link)

结果我有:

Jayanagar 2nd Block, 4000 sft Plot for Sale 9845012673
PRESTIGE OZONE TYPE D 3 B/R VILLA FOR SALE 9611226364
T ZED HOMES 3 BEDROOM FLAT FOR SALE 9611226364

结果我期待:

A Flat is for sale at  Cooke Town
Prestige Sunnyside
Jayanagar 2nd Block, 4000 sft Plot for Sale 9845012673
PRESTIGE OZONE TYPE D 3 B/R VILLA FOR SALE 9611226364
T ZED HOMES 3 BEDROOM FLAT FOR SALE 9611226364

2 个答案:

答案 0 :(得分:1)

请注意,例如,在http://bangalore.craigslist.co.in/reb/d/flat-is-for-sale-at-cooke-town/6266183606.html上,'a.showcontact'选择器没有匹配的链接,因此关注块

try:
    link = base + tree.cssselect('a.showcontact')[0].attrib['href']
except IndexError:
    link = ""

将返回link = ""

然后,当您致电if ano_page_link:时,if块中的所有命令都会被忽略,因为条件if ""False并且没有打印出任何内容

您可以尝试以下方式:

def parse_doc(title, ano_page_link):

    if ano_page_link:
        page = requests.get(ano_page_link).text            
        tel = re.findall(r'\d{10}', page)[0] if re.findall(r'\d{10}', page) else ""
        print(title, tel)
    else:
        print(title)

答案 1 :(得分:1)

通过将收集数据打印数据这两项任务分开,您可以获得更大的灵活性。当您想要扩展时,可以更轻松地添加更多信息。

def collect_info(medium_link):
    response = requests.get(medium_link).text
    tree = html.fromstring(response)

    title = get_title(tree)
    contact_link = get_contact_link(tree)
    tel = get_tel(contact_link) if contact_link else ''

    return title, tel


def get_title(tree):
    try:
        name = tree.cssselect('span#titletextonly')[0].text
    except IndexError:
        name = ""
    return name

def get_contact_link(tree):
    try:
        link = base + tree.cssselect('a.showcontact')[0].attrib['href']
    except IndexError:
        link = ""
    return link

def get_tel(ano_page_link):
    page = requests.get(ano_page_link).text
    tel = re.findall(r'\d{10}', page)[0] if re.findall(r'\d{10}', page) else ""
    return tel

def print_info(title, tel):
    if tel:
        fmt = 'Title: {title}, Phone: {tel}'
    else:
        fmt = 'Title: {title}'
    print(fmt.format(title=title, tel=tel))

if __name__ == '__main__':
    for link in url_list:
        title, tel = collect_info(link)
        print_info(title, tel)