Python一遍又一遍地在同一行打印两个结果

时间:2017-08-10 03:02:45

标签: python

在cmd中运行程序;打印功能

with open('test1.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)

    for index, url in enumerate(URL_LIST):
    page = requests.get(url)
    print '\r' 'Scraping URL ' + str(index+1) +   ' of  ' + str(len(URL_LIST)),

if text2search in page.text:
    tree = html.fromstring(page.content)
    (title,) = (x.text_content() for x in tree.xpath('//title'))
    (price,) = (x.text_content() for x in tree.xpath('//div[@class="property-value__price"]'))
    (sold,) = (x.text_content().strip() for x in tree.xpath('//p[@class="property-value__agent"]'))
    writer.writerow([title, price, sold])

返回:刮取网址1 of 400

反复计数结束。

我今天要学习的是,在2个单独的行上打印2个结果,一遍又一遍直到循环结束。

示例:

刮痧网址 1 400其中粗体字只是改变的东西

然后如果刮刀在列表中找到结果;

将结果 1 添加到CSV其中粗体字符只是更改的内容

到目前为止,我已经尝试了一些打印命令,但它要么在同一行上覆盖整个句子;

with open('test1.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)
    for index, url in enumerate(URL_LIST):
        page = requests.get(url)
        print '\r' 'Scraping URL ' + str(index+1) +   ' of  ' + str(len(URL_LIST)),

     if text2search in page.text:
        tree = html.fromstring(page.content)
        (title,) = (x.text_content() for x in tree.xpath('//title'))
        (price,) = (x.text_content() for x in tree.xpath('//div[@class="property-value__price"]'))
       (sold,) = (x.text_content().strip() for x in tree.xpath('//p[@class="property-value__agent"]'))
       writer.writerow([title, price, sold])
       print '\r' 'URL_FOUND' + str(index+1) + 'adding to CSV',

如果我尝试将两个打印函数链接到else参数,它将只打印第一个语句而第二个语句不会被确认。

with open('test1.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)
    for index, url in enumerate(URL_LIST):
        page = requests.get(url)
        print '\r' 'Scraping URL ' + str(index+1) +   ' of  ' + str(len(URL_LIST)),
else:
        if text2search in page.text:
        tree = html.fromstring(page.content)
        (title,) = (x.text_content() for x in tree.xpath('//title'))
        (price,) = (x.text_content() for x in tree.xpath('//div[@class="property-value__price"]'))
        (sold,) = (x.text_content().strip() for x in tree.xpath('//p[@class="property-value__agent"]'))
        writer.writerow([title, price, sold])
        print '\n' 'title'

只是想知道是否有人能指出我正确的方向在2行打印两个结果。

如果需要,请填写以下完整代码:

import requests
import csv
import datetime
import pandas as pd
import csv
from lxml import html

df = pd.read_excel("C:\Python27\Projects\REA_SCRAPER\\REA.xlsx", sheetname="REA")
dnc = df['Property']
dnc_list = list(dnc)
url_base = "https://www.realestate.com.au/property/"
URL_LIST = []

for nd in dnc_list:
    nd = nd.strip()
    nd = nd.lower()
    nd = nd.replace(" ", "-")
    URL_LIST.append(url_base + nd)

text2search = '''RECENTLY SOLD'''

with open('test1.csv', 'wb') as csv_file:
    writer = csv.writer(csv_file)

    for index, url in enumerate(URL_LIST):
        page = requests.get(url)
        print '\r' 'Scraping URL ' + str(index+1) +   ' of  ' + str(len(URL_LIST)),

        if text2search in page.text:
            tree = html.fromstring(page.content)
            (title,) = (x.text_content() for x in tree.xpath('//title'))
            (price,) = (x.text_content() for x in tree.xpath('//div[@class="property-value__price"]'))
            (sold,) = (x.text_content().strip() for x in tree.xpath('//p[@class="property-value__agent"]'))
            writer.writerow([title, price, sold])

1 个答案:

答案 0 :(得分:1)

我会推荐curses,但你在Windows上只是写一个看似小脚本的东西;足够的理由不要去那个兔子洞。

你看到你的线条相互覆盖的原因是因为你正在打印回车\r,它将光标移动到线的开头。此后写的任何文本都将覆盖以前打印的文本。

我发现this有一个快速的Google,您可能会感兴趣。