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