Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Scraper无法打印所有结果_Python_Python 3.x_Web Scraping_Web Crawler - Fatal编程技术网

Python Scraper无法打印所有结果

Python Scraper无法打印所有结果,python,python-3.x,web-scraping,web-crawler,Python,Python 3.x,Web Scraping,Web Crawler,我已经用python编写了一个脚本,从craigslist中抓取了五个项目的“姓名”和“电话”。我面临的问题是,当我运行脚本时,它只给出三个结果,而不是五个结果。更具体地说,由于前两个链接的页面中没有其他链接(联系信息),因此他们不需要再次请求打开任何其他页面。但是,这两个没有(联系信息)链接的链接不能通过我的第二个函数中的“if ano_page_link:”语句渗透,并且永远不会被打印出来。如何修复此缺陷,以便无论是否有电话号码,刮板都将打印所有五个结果 我正在尝试使用的脚本: import

我已经用python编写了一个脚本,从craigslist中抓取了五个项目的“姓名”和“电话”。我面临的问题是,当我运行脚本时,它只给出三个结果,而不是五个结果。更具体地说,由于前两个链接的页面中没有其他链接(联系信息),因此他们不需要再次请求打开任何其他页面。但是,这两个没有(联系信息)链接的链接不能通过我的第二个函数中的“if 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

请注意,例如,在上没有与
'a.showcontact'
选择器匹配的链接,因此下面的块

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

然后,当您调用
if ano\u page\u链接时:
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)

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

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)

您是否在
中为
循环定义函数?为什么?对不起,先生。我不该这么做。我是为这个演示做的。根据您的建议进行了修改。谢谢您的回答。它解决了这个问题。我也想到了“else”块,但我的无知不允许我这么做。哈哈!!。你是个救命恩人。再次感谢先生。先生,还有一件事需要知道。在正常情况下,当涉及到以csv格式写入数据时,我会在print statement“writer.writerow”([title,tel])后面或周围放一行“。但是,您能否建议我如何修改这一行,因为“标题”在此处出现两次(在打印声明中)。提前谢谢。我不确定,因为我的
.csv
有点过期,但如果没有页面链接,您可以尝试类似的
:…writer.writerow([title,tel])
其他:writer.writerow([title,“])
您的解决方案决不会出错。它成功了。再次谢谢,先生。