Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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在同一行上反复打印两个结果_Python - Fatal编程技术网

Python在同一行上反复打印两个结果

Python在同一行上反复打印两个结果,python,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 te

在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])
返回:抓取URL 1/400

一遍又一遍直到计数结束

我今天要学的是在两行上打印两个结果,反复打印直到循环结束

例如:

删除URL1,其中只有粗体字符在改变

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

将结果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',
如果我尝试将两个print函数链接到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'
只是想知道是否有人能为我指出在两行上打印两个结果的正确方向

如有需要,完整代码如下:

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])
我会推荐的,但你在Windows上,只是写一个小脚本;有足够的理由不去兔子洞

您看到行相互覆盖的原因是您正在打印回车号
\r
,这会将光标移动到行的开头。此后写入的任何文本都将覆盖以前打印的文本

我用谷歌搜索了一下,你可能会感兴趣