BBCのニュース記事の見出しと最初の段落を返すPythonコードがありますが、現在はリンクを提供する必要があります。コードは次のとおりです:Python - URLから見出しをスクラップしますが、URLはユーザー入力に由来します
from lxml import html
import requests
response = requests.get('http://www.bbc.co.uk/news/business-40660355')
if (response.status_code == 200):
pagehtml = html.fromstring(response.text)
news1 = pagehtml.xpath('//h1[@class="story-body__h1"]/text()')
news2 = pagehtml.xpath('//p[@class="story-body__introduction"]/text()')
print("\n".join(news1) + " (BBC News)")
print("\n".join(news2))
しかし、このコードは、私がrequest.get( '')ビットにURLをコピーすることに依存しています。
ここでは、ユーザの入力を可能にするためにそれを変更することで、私の試みです:
from lxml import html
import requests
response = input()
if (response.status_code == 200):
pagehtml = html.fromstring(response.text)
news1 = pagehtml.xpath('//h1[@class="story-body__h1"]/text()')
news2 = pagehtml.xpath('//p[@class="story-body__introduction"]/text()')
print("\n".join(news1) + " (BBC News)")
print("\n".join(news2))
しかし残念ながら、次のエラー戻ってきたこと:誰がための最善の方法を知っていれば
http://www.bbc.co.uk/news/world-europe-40825668
Traceback (most recent call last):
File "myscript2.py", line 5, in <module>
response = input()
File "<string>", line 1
http://www.bbc.co.uk/news/world-europe-40825668
^
SyntaxError: invalid syntax
を私は思っていましたURLから情報を取得するためにコードを変更するユーザーに頼るのではなく、入力を取ることによってこのコードが機能するようにします。ここで
おかげ
python3を使用していない限り、 'raw_input'が必要です。 – jordanm
また、私はあなたが何かの行に沿ってしたいと思います: '応答= requests.get(入力)' – tmarice
こんにちは@ヨルダン、私はPython 3.5を使用してありがとう –