我必须每天粘贴3000个未格式化的网址
我可以设置代码将原始粘贴数据转换为字符串吗?
(原始数据示例) - 13 Michael Way Cottees NSW 2017
(示例更改数据) - “13 Michael Way Cottees NSW 2017”
我试过了
RAW_URL = 13 Michael Way Cottees NSW 2017 + " "
RAW_URL = str(13 HOADLEY ST MAWSON ACT 2607)
RAW_DATA = ' " ' + (13 HOADLEY ST MAWSON ACT 2607) + ' " '
我一直收到“无效语法”错误,并且谷歌没有太多运气。
一旦完成,它将被折叠到下面的代码中,将PASTED_CRM_DATA上的单个输入替换为下面的列表
import requests
import csv
from lxml import html
import time
import sys
text2search = '''RECENTLY SOLD'''
PASTED_CRM_DATA = "13 HOADLEY ST MAWSON ACT 2607"
URL_LIST = 'https://www.realestate.com.au/property/' + str(PASTED_CRM_DATA.replace(' ', '-').lower()),
with open('REA.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))+ ' ' + url,
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])
赞赏任何输入
答案 0 :(得分:0)
首先,您应该了解python中的strings
在您尝试过的示例中
RAW_URL = 13 Michael Way Cottees NSW 2017 + " "
RAW_URL = str(13 HOADLEY ST MAWSON ACT 2607)
RAW_DATA = ' " ' + (13 HOADLEY ST MAWSON ACT 2607) + ' " '
这里,您尝试使用字符串的字符被解释为实际代码。为了让翻译人员明白你的意图,请在他们周围使用单引号'
。 (或双引号)
RAW_URL = '13 Micheal Way Cottees NSW 2017'
RAW_DATA = '13 HOADLEY SY MAWSON ACT 2607'
要应用引号,请使用字符串连接
RAW_URL = '"' + '13 Micheal Way Cottees NSW 2017' + '"'
我很不确定您对原始粘贴数据的意思。从哪里复制数据?是手动还是在程序中完成?