在python中从链接中提取文本
我用python 2.7编写了一个脚本,可以在本页中删除表格: 我想提取每个列,问题是我的代码无法识别有链接的列(第2列和第3列) 使用此代码,htmltable[0]是排名,htmltable[1]是生产预算,并从那里继续。 从那些我错过的,我需要的是文本,而不是链接在python中从链接中提取文本,python,python-2.7,web-scraping,Python,Python 2.7,Web Scraping,我用python 2.7编写了一个脚本,可以在本页中删除表格: 我想提取每个列,问题是我的代码无法识别有链接的列(第2列和第3列) 使用此代码,htmltable[0]是排名,htmltable[1]是生产预算,并从那里继续。 从那些我错过的,我需要的是文本,而不是链接 import urllib budgeturl = "http://www.the-numbers.com/movie/budgets/all" s = urllib.urlopen(budgeturl).read() d
import urllib
budgeturl = "http://www.the-numbers.com/movie/budgets/all"
s = urllib.urlopen(budgeturl).read()
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
s = find_between(s, '<table>', '</table>')
print s[:500]
print '.............................................................'
print s[-250:]
我需要的是文本而不是链接
import urllib
budgeturl = "http://www.the-numbers.com/movie/budgets/all"
s = urllib.urlopen(budgeturl).read()
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
s = find_between(s, '<table>', '</table>')
print s[:500]
print '.............................................................'
print s[-250:]
通过
您可以使用beautifulsoup执行相同的操作,请参见:
我需要的是文本而不是链接
import urllib
budgeturl = "http://www.the-numbers.com/movie/budgets/all"
s = urllib.urlopen(budgeturl).read()
def find_between( s, first, last ):
try:
start = s.index( first ) + len( first )
end = s.index( last, start )
return s[start:end]
except ValueError:
return ""
s = find_between(s, '<table>', '</table>')
print s[:500]
print '.............................................................'
print s[-250:]
通过
您可以使用beautifulsoup执行相同的操作,请参见:
您需要修改xpath,因为并非所有
td
元素都有class=“data”
。
尝试此xpath表达式://td//text()
输出:
您需要修改xpath,因为并非所有
td
元素都有class=“data”
。
尝试此xpath表达式://td//text()
输出:
你能不指定
class='data'
而直接抓取文本吗?看起来其他TDs没有类。不确定如何操作您可以在不指定class='data'
的情况下抓取文本吗?看起来其他的TDs没有课。不知道怎么做
.........................................
Release Date,Movie,Production Budget,Domestic Gross,Worldwide Gross
1,12/18/2009,Avatar,"$425,000,000","$760,507,625","$2,783,918,982"
8/5/2005,My Date With Drew,"$1,100","$181,041","$181,041"
import urllib
from lxml import etree
budgeturl = "http://www.the-numbers.com/movie/budgets/all"
s = urllib.urlopen(budgeturl).read()
htmlpage = etree.HTML(s)
htmltable = htmlpage.xpath("//td//text()")