用Python阅读网站上的每一行
我期待着阅读网页上的每一行,而有行。到目前为止,我有下面的代码。我无法让它为每一行指定temp值,我希望使用regex检查该行是否符合特定格式用Python阅读网站上的每一行,python,output,Python,Output,我期待着阅读网页上的每一行,而有行。到目前为止,我有下面的代码。我无法让它为每一行指定temp值,我希望使用regex检查该行是否符合特定格式 #!/usr/bin/python import urllib2 import re #imported urllib to collect the data. imported re for regular expressions to test format. #creating our output file f=open("OUI
#!/usr/bin/python
import urllib2
import re
#imported urllib to collect the data. imported re for regular expressions to test format.
#creating our output file
f=open("OUIoutput.txt", "w+")
#opening a file like object using urllib
webpage= urllib2.urlopen("https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf")
#string used to store the output
str1=""
#string used to store current line
temp=""
#while loop to read in the data for every line.INCORRECT FOR LOOP BASIC PLACEHOLDER IN THE CODE
for i in (60,500):
temp=webpage.readline(i)
if re.search("\w\w:\w\w:\w\w", temp):
str1+=temp
f.write(str1)
您可以使用单个调用re.findall,利用多行标志:
import requests
import re
pattern = re.compile(ur'^.*\w\w:\w\w:\w\w.*$', re.M)
url = "https://code.wireshark.org/review/gitweb?p=wireshark.git;a=blob_plain;f=manuf"
webpage = requests.get(url)
print u'\n'.join(pattern.findall(webpage.text)).encode('utf-8')
根据您的评论回答:
您不需要为此使用范围。功能就是你所看到的
for line in webpage.readlines():
#do your work here
你得到了什么?例外不需要的输出?@蜘蛛侠没有输出。。。但最重要的是,现在我需要弄清楚,当文件中有一行时,如何读取每一行?HTML文件?你想解析网页吗?因为读每一行cud意味着你也会得到页面src代码…用于网页中的行。readlines:打印行,可能会循环通过它们+1作为备选答案