作者busystudent (busystudent)
看板Python
标题[问题] 自动载入网页且分析问题
时间Fri Apr 1 23:19:58 2016
hi 最近写python遇到几个问题,想跟大家请教
1.我有一个txt档,内有多笔网址,这是我爬虫下来的随机网站,如下范例
http://goo.gl/hZM42U
http://goo.gl/fjJ0lG
http://goo.gl/N9HjLw
..........................等多笔资料
我希望在下面的url中,可以自动载入上述的网址,
import xml
from __future__ import division
import nltk, re, pprint
from urllib import urlopen
url = "
http://goo.gl/hZM42U"
text = urlopen(url).read()
你可以观察url = "
http://goo.gl/hZM42U ",这一行就可。
我想请问要如何自动把网址加入url这一行中呢?我原本是打算一次open这个txt档,可以是马上就出错了,
只好一笔笔手动贴上,还请大家帮我解答,谢谢。
--
Sent from my Windows
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 123.110.158.25
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/Python/M.1459524000.A.B07.html
1F:→ busystudent: 再问个延伸问题到底要如何把 例如'aaa'这类型的物件塞进 ure = []的04/01 23:23
2F:→ busystudent: 框框里?04/01 23:23
※ 编辑: busystudent (42.72.191.2), 04/01/2016 23:28:06
3F:推 drajan: 可以叙述得详细点吗?这听起来像是ure + ['aaa']就好 04/01 23:56
4F:→ drajan: 你只要用个array塞进这些网址 然後iterate就好了 04/01 23:58
5F:→ busystudent: ok 问题点在於我有很多像是'aaa' 'bb'等 04/02 00:00
6F:→ busystudent: 的网页,可是我不会让这些网页一次ㄧ个载 04/02 00:00
7F:→ busystudent: 入 04/02 00:00
8F:→ busystudent: 我希望一次再入一个网址,我後续还有分析,等run完再 04/02 00:07
9F:→ busystudent: 跑第二个。 04/02 00:07
10F:→ checkIO: For I in my list: url=i 04/02 09:48
11F:→ octantis: 看你的描述应该可以用generator, 一次跑一个网址 04/02 12:08
12F:→ octantis: 重看一次不用generator, 就用for回圈跑就好 04/02 12:20
13F:→ MOONY135: 三楼的解应该就是正解了 04/02 13:38
14F:→ busystudent: 感谢大家的回应,谢谢 04/02 14:24
15F:→ kenduest: 题外话个人建议用 requests 取代内建那个 urllib 更好用 04/02 14:49
16F:→ kenduest: 若你用 python3 urllib 会有异动,程式码也要改 04/02 14:50
17F:推 drajan: 又载入xml又载入re又载入nltk...你是想要撷取资讯? 可以用 04/02 16:11
18F:→ drajan: BeautifulSoup...会简单很多 04/02 16:11