2017-08-10 107 views
0

在cmd中运行程序;打印功能Python在同一行上反复打印两个结果

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 400 1

一遍又一遍,直到计数结束。

我今天想学的东西是在2个单独的行上打印2个结果,一遍又一遍地循环结束。

例子:

刮URL 1的400,在大胆的性格是唯一改变的事情

这时如果刮刀在列表中查找的结果;

相加的结果到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', 

如果我尝试链接到两个打印功能的其他参数,将只打印第一条语句,二是不承认。

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]) 

回答

1

我会推荐curses,但你使用的是Windows,只是写这似乎是一个小的脚本;理由不足以让兔子洞下去。

你看到你的行相互覆盖的原因是因为你正在打印回车符\r,它将光标移动到行首。之后写入的任何文本都将覆盖先前的打印文本。

我发现this与一个快速的谷歌,这可能是你感兴趣的。