URLのリストをインポートし、ソースコードでいくつかのことをチェックするスクリプトに取り組んでいます。.csv のインポートと処理について助けが必要です。誰かがここで助けてくれるなら、コードの一部です
from lxml import html
import csv
def main():
with open('urls.csv', 'r') as csvfile:
urls = [row[0] for row in csv.reader(csvfile)]
for url in urls:
doc = html.parse(url)
linkziel = 'http://dandydiary.de/de'
if doc.xpath('//a[@href=$url]', url=linkziel):
for anchor_node in doc.xpath('//a[@href=$url]', url=linkziel):
if anchor_node.xpath('./ancestor::div[contains(@class, "sidebar")]'):
print 'Sidebar'
elif anchor_node.xpath('./parent::div[contains(@class, "widget")]'):
print 'Sidebar'
elif anchor_node.xpath('./ancestor::div[contains(@class, "comment")]'):
print 'Kommentar'
elif anchor_node.xpath('./ancestor::div[contains(@id, "comment")]'):
print 'Kommentar'
elif anchor_node.xpath('./ancestor::div[contains(@class, "foot")]'):
print "Footer"
elif anchor_node.xpath('./ancestor::div[contains(@id, "foot")]'):
print "Footer"
elif anchor_node.xpath('./ancestor::div[contains(@class, "post")]'):
print "Contextual"
else:
print 'Unidentified Link'
else:
print 'Link is Dead'
if __name__ == '__main__':
main()
URLを1つだけ指定する代わりに、実行されるcsvを使用したい(私はPython 2を使用しています)